Ani*_*n J 22 javascript modularity angularjs
我是angularjs的新手.我正在构建一个应用程序,其中我的主页将有一个帖子列表.我想过将angularjs单独用于双向绑定.我从一个示例页面开始,但在这里遇到了问题.
我的示例页面.我正在为这个div使用ng-app,因为我不希望angularjs与页面的任何其他方面进行交互.
<div ng-app="posts">
<ul class="post_list" ng-controller="PostController">
<li ng-repeat="post in posts">
{{post.title}}
{{post.description}}
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
我有一个app.js文件
var app = angular.module('posts', []);
Run Code Online (Sandbox Code Playgroud)
和一个postcontroller.js文件
(function(angular, app) {
// Define the Controller as the constructor function.
console.log(app);
console.log(angular);
app.controller("PostController",["$scope"], function($scope) {
$scope.posts = [
{"title":"asdf", "description":"describe"},
{"title":"second one", "description":"second describe"},
];
});
})(angular, app);
Run Code Online (Sandbox Code Playgroud)
当我加载我的主页.我正进入(状态
错误:参数"PostController"不是函数.得到字符串[googlecdn path] angular.min.js:16
我在这里缺少什么 请帮助我,因为我完全困惑.我是angularjs的新手,也是javascript的新手.
pko*_*rce 48
问题在于您声明控制器的方式.你应该这样做如下所示:
app.controller("PostController",["$scope", function($scope) {
$scope.posts = [
{"title":"asdf", "description":"describe"},
{"title":"second one", "description":"second describe"},
];
}]);
Run Code Online (Sandbox Code Playgroud)
请注意,第二个参数是一个数组,其中包含$scope字符串和函数作为数组的元素.这是用于编写简化安全代码的正确语法.
| 归档时间: |
|
| 查看次数: |
22049 次 |
| 最近记录: |