基于html值显示glyphicon
控制器:基于html值显示glyphicon,html,angularjs,Html,Angularjs,控制器: .controller('BlogController', function(blogFactory, $routeParams, $scope){ var that=this; stat=false; this.checkbookmark = function(bId){ console.log(bId) blogFactory.checkBookmark(bId, function(response){ console.
.controller('BlogController', function(blogFactory, $routeParams, $scope){
var that=this;
stat=false;
this.checkbookmark = function(bId){
console.log(bId)
blogFactory.checkBookmark(bId, function(response){
console.log(response)
if(response == "bookmarked"){
that.stat = true;
}
else{
that.stat = false;
}
})
}
html代码:
<div class="container" ng-controller="BlogController as blogCtrl">
<div class="row" ng-repeat="chunk in blogCtrl.blogs | filter: filter_name | orderBy:'-created_at' | groupBy: 3">
<div class="outerbox1 col-sm-4" ng-repeat="blog in chunk" >
<div class="innerbox3" ng-init="blogCtrl.checkbookmark(blog._id)">
<br>
<div> > READ MORE
<a ng-if="blogCtrl.stat" ng-href="#" ng-click="blogCtrl.removebookmark(blog._id)" class="glyphicon glyphicon-heart pull-right">{{blogCtrl.stat}}</a>
<a ng-if="!blogCtrl.stat" ng-href="#" ng-click="blogCtrl.addbookmark(blog._id)" class="glyphicon glyphicon-heart-empty pull-right">{{blogCtrl.stat}}</a>
</div>
</div>
</div>
</div>
</div>
>阅读更多
我想根据stat
我有6个博客,前3个是书签,下3个不是
我遇到的问题是,stat值总是根据最后一个书签设置的,因此它总是false/true(基于最后一个博客的条件)
如何解决这个问题?您不应该在控制器上设置stat属性,而应该在blog对象上设置属性(它显然属于该对象) 将checkbookmark功能替换为以下内容:
this.checkbookmark = function(blog){ //pass the entire blog, not just the id
blogFactory.checkBookmark(blog._id, function(response){
console.log(response)
if(response == "bookmarked"){
blog.stat = true; //set the property on blog instead of the controller
}
else{
blog.stat = false;
}
})
}
然后像这样使用它:
<div class="innerbox3" ng-init="blogCtrl.checkbookmark(blog)">
<br>
<div> > READ MORE
<a ng-if="blog.stat" ng-href="#" ng-click="blogCtrl.removebookmark(blog._id)" class="glyphicon glyphicon-heart pull-right">{{blog.stat}}</a>
<a ng-if="!blog.stat" ng-href="#" ng-click="blogCtrl.addbookmark(blog._id)" class="glyphicon glyphicon-heart-empty pull-right">{{blog.stat}}</a>
</div>
</div>
>阅读更多
您需要对
add
和removebookmark
功能进行类似的更改,这样才能正常工作。谢谢现在,您能告诉我如何实时更新显示,即,如果用户单击,glyphicon图标将发生更改@X4RF41正如我所说,您需要对add和remove函数进行相同的更改:传递整个blog而不仅仅是id,然后将blog.stat属性设置为新值