为什么ng-repeat-start和ng-repeat- end不能正常工作
Why ng-repeat-start and ng-repear-end not working correctly?
我试图用Angular JS(也使用Laravel)构建一个简单的注释应用程序,这里是一个代码:
<div ng-controller="CommentController">
<div ng-repeat-start="comment in comments" class="comment">
<h3>Comment {{ comment.id }} <small>by {{ comment.author }}</small></h3>
<p>{{ comment.text }}</p>
</div>
<div ng-repeat-end></div>
</div>
</div>
这里是我的JS文件:
function CommentController($scope) {
$scope.comments = [
{ id: 1 },
{ author: "Artem" },
{ text: "Angular comment" }
];
}
输出:
Comment 1 by
Artem评论
评论
角度评论
您的JSON对象可能格式错误。
从你想要达到的,它应该是下面的格式:
$scope.comments = [
{ id: 1, author: "Artem", text: "Angular comment" },
{ id: 2, author: "Other", text: "Other Angular comment" }
];
您的控制器似乎没有注册。你应该这样做:
myApp.controller('CommentController', ['$scope', function($scope) {
$scope.comments = [
{ id: 1, author: "Artem", text: "Angular comment" },
{ id: 2, author: "Other", text: "Other Angular comment" }
];
}]);
只把myApp改成你的模块名
1。这里不需要ng-repeat-start和ng-repeat-end指令。这些指令背后有什么特别的原因吗?
2。可以通过使用ng-repeat来实现。
如果你想使用这两个指令。试试这个
<div ng-controller="CommentController">
<div ng-repeat-start="comment in comments" class="comment">
<h3>Comment {{ comment.id }} <small>by {{ comment.author }}</small></h3>
</div>
<div ng-repeat-end></div>
<p>{{ comment.text }}</p>
</div>
</div>
OR,简单的方法是:
<div ng-controller="CommentController">
<div ng-repeat="comment in comments" class="comment">
<h3>Comment {{ comment.id }} <small>by {{ comment.author }}</small></h3>
<p>{{ comment.text }}</p>
</div>
</div>
角医生
3。您的JSON
格式也可能是错误的。Angular将每个值视为对象。如果你想让object
有三个值,像这样改变你的JSON
。
function CommentController($scope) {
$scope.comments = [
{ id: 1 ,
author: "Artem",
text: "Angular comment" }
];
}
@zorza提到的可能也是正确的。
除非在你的代码下面的某个地方你使用了像下面这样的东西:
myApp.controller('CommentController', 'CommentController');
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- ajaxToolkit PopupControlExtender不工作.过时的
- HTML标记在脚本标记中工作
- javascript扫雷器floodfill算法不能正常工作