我想用 ons-lazy-repeat 但是有错误
I want to use ons-lazy-repeat but there is a error
我的代码如下:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize'],['infinite-scroll']);
module.controller('NewsController', ['$scope', '$http', '$q','$timeout', function($scope, $timeout, $http, Demo, $q) {
$scope.MyDelegate = {
configureItemScope: function(index, itemScope) {
if (!itemScope.item) {
itemScope.canceler = $q.defer();
itemScope.item = {
title: 'Item #' + (index + 1),
label: '',
desc: '',
rand: Math.random()
};
$http.get('https://baconipsum.com/api/?type=meat-and-filler&sentences=1', {
timeout: itemScope.canceler.promise
}).success(function(data) {
itemScope.item.desc = data[0];
itemScope.item.label = itemScope.item.desc.substr(0, itemScope.item.desc.indexOf(" ")) + 'bacon'
}).error(function() {
itemScope.item.desc = 'No bacon lorem ipsum';
itemScope.item.label = 'No bacon'
});
}
},
calculateItemHeight: function(index) {
return 91;
},
countItems: function() {
return 10000000;
},
destroyItemScope: function(index, itemScope) {
itemScope.canceler.resolve();
}
};
)]};
其实我只是从http://onsen.io/blog/onsenui-1-2-2-new-components-lazy-repeat/
复制了ons-lazy-repeat的文档
错误是:错误:undefined 不是对象(正在评估“$q.defer”)
有人知道这个问题的原因吗?
您错误地将依赖项注入了控制器。依赖项的字符串名称的顺序必须 完全匹配 函数导入的顺序。你有什么:
module.controller('NewsController', ['$scope', '$http', '$q', '$timeout',
function($scope, $timeout, $http, Demo, $q) {
你应该拥有的东西:
module.controller('NewsController', ['$scope', '$http', '$q', '$timeout',
function($scope, $http, $q, $timeout) {
此外,您将 Demo
作为函数导入,但没有在导入中列出 'Demo'
;你似乎没有在使用它,但如果你在使用它,你应该确保你有两个,按照匹配的顺序。
此外,我不确定为什么要将多个数组注入到您的应用程序模块中:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize'],['infinite-scroll']);
这可能会或可能不会正确工作,但这绝对不是标准语法。应该是:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize','infinite-scroll']);
我的代码如下:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize'],['infinite-scroll']);
module.controller('NewsController', ['$scope', '$http', '$q','$timeout', function($scope, $timeout, $http, Demo, $q) {
$scope.MyDelegate = {
configureItemScope: function(index, itemScope) {
if (!itemScope.item) {
itemScope.canceler = $q.defer();
itemScope.item = {
title: 'Item #' + (index + 1),
label: '',
desc: '',
rand: Math.random()
};
$http.get('https://baconipsum.com/api/?type=meat-and-filler&sentences=1', {
timeout: itemScope.canceler.promise
}).success(function(data) {
itemScope.item.desc = data[0];
itemScope.item.label = itemScope.item.desc.substr(0, itemScope.item.desc.indexOf(" ")) + 'bacon'
}).error(function() {
itemScope.item.desc = 'No bacon lorem ipsum';
itemScope.item.label = 'No bacon'
});
}
},
calculateItemHeight: function(index) {
return 91;
},
countItems: function() {
return 10000000;
},
destroyItemScope: function(index, itemScope) {
itemScope.canceler.resolve();
}
};
)]};
其实我只是从http://onsen.io/blog/onsenui-1-2-2-new-components-lazy-repeat/
复制了ons-lazy-repeat的文档错误是:错误:undefined 不是对象(正在评估“$q.defer”)
有人知道这个问题的原因吗?
您错误地将依赖项注入了控制器。依赖项的字符串名称的顺序必须 完全匹配 函数导入的顺序。你有什么:
module.controller('NewsController', ['$scope', '$http', '$q', '$timeout',
function($scope, $timeout, $http, Demo, $q) {
你应该拥有的东西:
module.controller('NewsController', ['$scope', '$http', '$q', '$timeout',
function($scope, $http, $q, $timeout) {
此外,您将 Demo
作为函数导入,但没有在导入中列出 'Demo'
;你似乎没有在使用它,但如果你在使用它,你应该确保你有两个,按照匹配的顺序。
此外,我不确定为什么要将多个数组注入到您的应用程序模块中:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize'],['infinite-scroll']);
这可能会或可能不会正确工作,但这绝对不是标准语法。应该是:
var module = ons.bootstrap('my-app', ['onsen','ngSanitize','infinite-scroll']);