Phi*_*rek 16 javascript runtime-error promise deferred angularjs
我正在尝试让我的应用程序在更改路线之前收集数据,如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
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
路线 -
angular.module( 'saApp' )
.config( function ( $routeProvider, $locationProvider ) {
$routeProvider
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: Dashboard,
resolve: Dashboard.resolve
}
});
Run Code Online (Sandbox Code Playgroud)
控制器 -
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,它执行解析,DBFactory资源继续,返回并且一切正常,直到实际的"deferred.resolve(data);" 这是我得到上面粘贴的错误的地方.如果我评论一行,当然我没有页面,但我也没有错误.
从DBFactory返回的数据内容是一个JSON对象,这是预期的,并在我在网上和视频中看到的所有示例中都有显示.
使用:
AngularJS 1.0.6
Run Code Online (Sandbox Code Playgroud)
思考?感谢你的帮助.
更新:
这似乎是我使用我的路由并使用命名空间定义控制器的方式:
var Dashboard = angular.module( 'saApp' ).controller()
Run Code Online (Sandbox Code Playgroud)
应该归咎于此.当我只使用标准函数声明时:
function DashboardCtrl($scope, dataset) {
$scope.data = dataset;
}
Run Code Online (Sandbox Code Playgroud)
它满足"寻找功能,得到对象"的错误.奇怪的是我改为使用它作为对象"var DashboardCtrl = angular.module('saApp').controller()"或者甚至是我之前从yeoman生成器那里得到的,并在angularjs docs中指定的:
angular.module( 'saApp' )
.controller( 'DashboardCtrl', function ( $scope, dataset ) {});
Run Code Online (Sandbox Code Playgroud)
有一条路线:
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: 'DashboardCtrl'
} )
Run Code Online (Sandbox Code Playgroud)
不行.
我多次出现这个错误,终于找到了完美而简单的解决方案.只需将控制器名称在'when'构造中放入引号,如下所示:
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: 'Dashboard',
resolve: Dashboard.resolve
}
Run Code Online (Sandbox Code Playgroud)
代替
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: Dashboard,
resolve: Dashboard.resolve
}
Run Code Online (Sandbox Code Playgroud)
我遇到过同样的问题。您可以通过执行以下操作来解决它。
从路由 .when() 方法中删除控制器声明。所以这...
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
controller: Dashboard,
resolve: Dashboard.resolve
}
Run Code Online (Sandbox Code Playgroud)
就变成这样了……
.when( '/dashboard', {
templateUrl: 'views/dashboard/dashboard.html',
resolve: Dashboard.resolve
}
Run Code Online (Sandbox Code Playgroud)
现在为了维护您的范围,请在 html 模板中添加控制器声明。例如..
<div ng-controller="DashboardCtrl">{{Some fancy dashboard stuff}}</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50612 次 |
| 最近记录: |