[更新] 这对我来说是一个愚蠢的错字。谢谢dfsq!
这里有一个类似的问题,我的代码遵循推荐的答案,所以请不要将其标记为重复。
在我的localhost中,我收到以下错误:Error: [ng:areq] Argument 'PostCtrl' is not a function, got undefined
app.js的内容:
/* global app:true */
/* exported app */
'use strict';
var app = angular
.module('redjsApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'firebase'
])
.constant('FIREBASE_URL', 'https://blazing-fire-6602.firebaseio.com/')
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/posts.html',
controller: 'PostCtrl'
})
.when('/about', {
templateUrl: 'views/about.html',
controller: 'AboutCtrl'
})
.otherwise({
redirectTo: '/'
});
});
脚本/服务/ post.js的内容:
'use strict';
app.factory('Post', function ($firebase, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL + 'posts');
var posts = $firebase(ref.child('posts')).$asArray();
console.log($firebase(ref.child('posts')).$asArray());
var Post = {
all: posts,
create: function (post) {
return posts.$add(post);
},
get: function (postId) {
return $firebase(ref.child('posts').child(postId)).$asObject();
},
delete: function (post) {
return posts.$remove(post);
}
};
return Post;
});
scripts / posts.js的内容:
'use strict';
app.controller('PostsCtrl', function ($scope, Post) {
$scope.posts = Post.all;
$scope.post = {url: 'http://', 'title': ''};
$scope.submitPost = function () {
Post.create($scope.post).then(function () {
$scope.post = {url: 'http://', 'title': ''};
});
};
$scope.deletePost = function (post) {
Post.delete(post);
};
});
posts.html的内容是:
<div ng-repeat="post in posts">
<a ng-href="{{ post.url }}">{{ post.title }}</a>
<a ng-click="deletePost(post)">delete</a>
</div>
</div>
<form ng-submit="submitPost()">
<input type="text" ng-model="post.title" />
<input type="text" ng-model="post.url" />
<input type="submit" value="Add Post" />
</form>
Posts: {{ posts}} <br />
Post: {{post}}
只渲染html,没有关联的js被渲染。应用程序是使用yeoman搭建的。帮助将不胜感激!
答案 0 :(得分:3)
您的控制器名称有拼写错误:
app.controller('PostsCtrl', ...
在路线中
controller: 'PostCtrl'