角度.js在范围更新后运行 ng-show 函数
Angular.js run ng-show function after scope is updated
我有一个投票应用程序,用户可以在其中为给定民意调查中的选项投票。 在 HTML 模板中,我使用 ng-show
来显示用户投票支持此选项或此投票的天气,或者是否是用户的未投票投票:
<div data-ng-repeat="option in poll.poll_options" class="list-group-item">
<span data-ng-if="option.option_thumb == '2'" class="glyphicon glyphicon-thumbs-up"></span>
<span data-ng-if="option.option_thumb == '1'" class="glyphicon glyphicon-thumbs-down"></span>
<div data-ng-show="optionVoted(option,authentication.user._id)">
<span data-ng-bind="option.option_text"></span>
</div>
<div data-ng-hide="optionVoted(option,authentication.user._id)">
<div data-ng-show="pollVoted(poll._id,votes)">
<a data-ng-click="updateVote()">
<span data-ng-bind="option.option_text"></span> - update
</a>
</div>
<div data-ng-hide="pollVoted(poll._id,votes)">
<a data-ng-click="createVote(poll,option,authentication.user._id,$index)">
<span data-ng-bind="option.option_text"></span> - new
</a>
</div>
</div>
<span class="option-votes"> - {{option.votes.length}}</span>
</div>
这些是上面提到的功能,用于确定选项/投票是否已由用户投票:
// check if option is voted
$scope.optionVoted = function(option,userId){
for (var i = 0; i < option.votes.length; i++){
if (option.votes[i].user === userId){
return true;
}
}
};
//check if poll is voted
$scope.pollVoted = function(pollId,votes){
for (var i = 0; i < votes.length; i++){
if (votes[i].poll === pollId){
return true;
}
}
}
这是创建新投票的函数:
// create a vote
$scope.createVote = function(poll,option,userId,index){
var vote = new Votes({
user:userId,
poll:poll._id,
poll_option:option._id
});
vote.poll_option_id = option._id;
vote.$save(function(vote){
option.votes.push(vote);
$scope.$apply();
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
}
前端发生的情况是,现在已经投票的选项被更新了(不再显示A标签)。 我需要的是,民意调查中的其他选项也会更新,现在它们不会create()
而是显示update()
,而无需刷新页面。
如何更新轮询中选项的其他 HTML DOM 元素?
在 html 中,将 ng-show 中的函数替换为对象属性:
例如,ng-show="option.voted"。
并更新选项.vote在创建投票功能中。
(使用用户ID等进行调整)
确保将新投票推送到作用域中的正确对象上。看起来您正在视图中显示来自$scope.poll.poll_options
的数据,但您正在createVote
函数中添加options.votes
。
相关文章:
- 使用angularjs ng Show/ng hide显示和隐藏不同的内容
- 我需要NG-IF/NG-SWITCH还是NG-SHOW&NG-HIDE
- 如何在AngularJS视图中使用promise(ng-show)
- AngularJS:如何使用java脚本函数更改跨度的ng-show条件
- ng show和ng hide don'I don’我一秒钟都不工作
- AngularJS:使用ng-show/hide动态维护下拉列表的3种状态(InProgress、Success、Err
- ng-show内容在页面加载过程中闪烁,尽管它不是真实的,并且ng-cloak不适用于FF
- 如何验证使用 ng-repeat、ng-show(角度)动态创建的输入
- 如何使用ng-repeat与清单模型进行ng-show
- AngularJS:ng-show不适用于ng-app
- 为什么我的html节点保持类ng隐藏属性ng show=true
- AngularJS:为什么ng show没有在IE11上运行
- 为预先选中的复选框在加载时运行ng更改函数
- Angularjs ng-show not working with checkbox with default val
- Angularjs - grouping ng show
- AngularJS:隐藏<td>使用ng-hide/ng-show没有间隙
- Angularjs ng-show == Id?
- 基于参数(angular)调用特定元素上的ng show
- 角度.js在范围更新后运行 ng-show 函数
- 单击时运行ng repeat(当ng show为true时)