我正在尝试为我的应用程序创建一个“喜欢”功能。我希望能够将动态生成的数字的值设置为“喜欢计数”。问题在于使用“ng-init”,因为文档说这是一种不好的方法!
您如何在“控制器”而不是“视图”中设置值?
这是我到目前为止所拥有的:
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<article ng-repeat="feed in feeds">
<h3>{{feed.createdby}}</h3>
<p>{{feed.content}}</p>
<button ng-click="likeClicked($index)">{{isLiked[$index]|liked}}</button>
<span ng-init="likeCount=feed.likes.length">{{likeCount}}</span>
</article>
</body>
</html>
谢谢,
J.P