当我尝试添加其他控制器时,我收到以下错误。我有一个名为“main”的控制器,我正在尝试添加一个额外的控制器“帖子”。
当我添加“posts”控制器时,我收到以下错误:
Uncaught SyntaxError: Unexpected token .
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.19/$injector/modulerr?p0=flapperNews&p1=Err…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.19%2Fangular.min.js%3A18%3A139)(anonymous function) @ angular.js:36(anonymous function) @ angular.js:3857q @ angular.js:325e @ angular.js:3823dc @ angular.js:3763c @ angular.js:1415cc @ angular.js:1430Xc @ angular.js:1343(anonymous function) @ angular.js:21773a @ angular.js:2549(anonymous function) @ angular.js:2822q @ angular.js:325c @ angular.js:2821
app.js
angular.module('flapperNews', ['ui.router'])
// Set routing/configuration
// ------------------------------
.config(['$stateProvider', '$urlRouterProvider',
// Set state providers
function($stateProvider, $urlRouterProvider) {$stateProvider
// Home state
.state('home', {
url: '/home',
templateUrl: '/static/home.html',
controller: 'MainCtrl'
})
// Posts state
.state('posts', {
url: '/posts{id}',
templateUrl: '/static/posts.html',
controller: 'PostsCtrl'
})
$urlRouterProvider.otherwise('home');
}
])
// Posts factory
// Factories are used to organize and share code across the app.
// ------------------------------
.factory('posts', [function(){
// create new obect with array of posts
var o = { posts: [] };
return o;
}])
// Main controller
// ------------------------------
.controller('MainCtrl', ['$scope', 'posts',
// Main scope (used in views)
function($scope, posts){
// array of posts
$scope.posts = posts.posts;
// Add post function
$scope.addPost = function(){
// prevent empty titles
if(!$scope.title || $scope.title === '') {
return;
}
// push new post to array
$scope.posts.push({title: $scope.title, date: new Date().toJSON().slice(0,10)});
// reset title
$scope.title = '';
};
}
]);
// Posts controller
// ------------------------------
.controller('PostsCtrl', ['$scope', '$stateParams', 'posts',
// Main scope (used in views)
function($scope, $stateParams, posts){
// body
}
]);
我一直在关注this教程,他们建议使用ui.router,所以我不确定这是否与它有关?
如果我在声明控制器之前添加应用程序名称,路由工作正常,但后来又出现了另一个错误:
Uncaught ReferenceError: flapperNews is not defined(anonymous function) @ app.js:43
angular.js:9959Error: [ng:areq] http://errors.angularjs.org/1.2.19/ng/areq?p0=MainCtrl&p1=not%20aNaNunction%2C%20got%20undefined
at Error (native)
答案 0 :(得分:1)
;
之前.controller
正在打破链
]); /* <=== remove ";" */
// Posts controller
// ------------------------------
.controller('PostsCtrl', ['$scope', '$stateParams', 'posts',
使用完整语法:
可能会更好angular.module('flapperNews').controller ...
angular.module('flapperNews').factory ...
答案 1 :(得分:0)
您错过了;
// Posts state
.state('posts', {
url: '/posts{id}',
templateUrl: '/static/posts.html',
controller: 'PostsCtrl'
}) ; // <---
$urlRouterProvider.otherwise('home');