我试图让我的应用程序在更改路线之前收集数据,如 John Lindquist 的许多视频所示:http://www.youtube.com/watch?v=P6KITGRQujQ&list=UUKW92i7iQFuNILqQOUOCrFw&index=4&feature=plcp
我已将其全部连接起来,但是当需要解析延迟对象时,我收到错误消息:
Error: Argument 'fn' is not a function, got Object
at assertArg (http://localhost:9000/components/angular/angular.js:1019:11)
at assertArgFn (http://localhost:9000/components/angular/angular.js:1029:3)
at annotate (http://localhost:9000/components/angular/angular.js:2350:5)
at invoke (http://localhost:9000/components/angular/angular.js:2833:21)
at Object.instantiate (http://localhost:9000/components/angular/angular.js:2874:23)
at http://localhost:9000/components/angular/angular.js:4759:24
at <error: illegal access>
at Object.Scope.$broadcast (http://localhost:9000/components/angular/angular.js:8261:28)
at http://localhost:9000/components/angular/angular.js:7417:26
at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59) angular.js:5704
我的代码是这样的:
路线-
angular.module( 'saApp' )
.config( function ( $routeProvider, $locationProvider ) {
$routeProvider
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: Dashboard,
resolve: Dashboard.resolve
}
});
Controller -
var Dashboard = angular.module( 'saApp' ).controller(
function ( $scope, dataset ) {
$scope.data = dataset;
} );
Dashboard.resolve = {
dataset: function ( $q, DBFactory ) {
console.log("dataset enter")
var deferred = $q.defer();
DBFactory.get( {noun: "dashboard"},
function ( data ) {
console.log("resolving");
deferred.resolve( data );
} );
console.log("promise");
return deferred.promise;
}
}
在运行时,它执行解析,DBFactory 资源运行,返回,一切正常,直到实际的“deferred.resolve(data);”这就是我在上面粘贴错误的地方。如果我评论那一行,当然我没有得到任何页面,但我也没有得到错误。
从 DBFactory 返回的数据内容是一个 JSON 对象,这是预期的,并且在我在网上看到的所有示例和视频中都有显示。
使用:
AngularJS 1.0.6
想法?感谢您的帮助。
更新:
这似乎是我使用路由并使用命名空间定义 Controller 的方式:
var Dashboard = angular.module( 'saApp' ).controller()
对此负有责任。当我简单地使用标准函数声明时:
function DashboardCtrl($scope, dataset) {
$scope.data = dataset;
}
它满足了“寻找函数,得到对象”的错误。奇怪的是我改为将它用作对象“var DashboardCtrl = angular.module('saApp').controller()”或者甚至是我之前从 yeoman 生成器中得到的,并在 angularjs 文档中指定的:
angular.module( 'saApp' )
.controller( 'DashboardCtrl', function ( $scope, dataset ) {});
路线为:
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: 'DashboardCtrl'
} )
不会工作。
最佳答案
我多次遇到这个错误,最终找到了完美而简单的解决方案。只需将您的 Controller 名称放在 'when' 结构中,像这样用引号引起来:
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: 'Dashboard',
resolve: Dashboard.resolve
}
代替
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: Dashboard,
resolve: Dashboard.resolve
}
关于javascript - AngularJS 延迟错误 : Argument 'fn' is not a function, 得到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155022/