【发布时间】:2013-04-15 19:43:40
【问题描述】:
我正在尝试让我的应用在更改路线之前收集数据,正如 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
}
});
控制器 -
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
想法?感谢您的所有帮助。
更新:
看来我使用路由并使用命名空间定义控制器的方式:
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'
} )
行不通。
【问题讨论】:
-
你试过使用 angular 1.0.5
-
试一试,同样的错误
-
我也坚持这个!我想在 $routeProvider.when() 的第二个参数的 route.resolve 属性中引用控制器方法。使用控制器定义的模块样式时我该怎么办? (也使用 yeoman)我被卡住了!
-
我在使用 CoffeeScript
module ($provide) -> $provide.provider 'XX', -> $get: -> "something"时遇到了类似的错误,因为 CoffeeScript 返回最后一个值,该值被注入器误解了。我在要解决的语句后添加了null。 -
由于控制器获取对象而不是函数而发生错误。我遇到了这个问题,因为 ES6 类已被声明为没有“导出默认值”。
标签: javascript angularjs runtime-error promise deferred