[英]Angular ng-if not responding as expected
I have li
elements that have an ng-if
statement that is tied to a checkbox. 我有li
元素具有绑定到复选框的ng-if
语句。 If I physically check the checkbox, the ng-if
responds as expected. 如果我亲自检查该复选框,则ng-if
按预期响应。 However, if I check or uncheck the checkbox via JavaScript, the checkboxes get checked or unchecked as expected, but the ng-if
does not respond. 但是,如果我通过JavaScript选中或取消选中该复选框,则复选框将按预期方式被选中或取消选中,但是ng-if
不会响应。 Is this working as it should? 这工作正常吗? Is there a better way to do this? 有一个更好的方法吗?
Here are my code samples: "[]" = "<>" 这是我的代码示例:“ []” =“ <>”
HTML - Part 1 HTML-第1部分
[li id="homebutton" ng-if="homechecked != true" onclick="homeclicked()"][a ng-href="/"][img src="images\\Neck01Home.png" alt="Home" /][/a] [li id =“ homebutton” ng-if =“ homechecked!= true” onclick =“ homeclicked()”] [a ng-href =“ /”] [img src =“ images \\ Neck01Home.png” alt =“主页“ /][/一种]
[li id="thebandbutton" ng-if="homechecked===true" onclick="thebandclicked()"][a ng-href="/theband/"][img src="images\\Neck01TheBand.png" alt="The Band" /][/a] [li id =“ thebandbutton” ng-if =“ homechecked === true” onclick =“ thebandclicked()”] [一个ng-href =“ / theband /”] [img src =“ images \\ Neck01TheBand.png” alt =“乐队” /] [/ a]
HTML - Part 2 HTML-第2部分
[label class="menucontroller"]Home2: [input class="menucontroller" id="homecb" type="checkbox" ng-model="homechecked" ng-init="homechecked=true" /][/label][br /] [label class =“ menucontroller”] Home2:[input class =“ menucontroller” id =“ homecb” type =“ checkbox” ng-model =“ homechecked” ng-init =“ homechecked = true” /] [/ label] [ br /]
[label class="menucontroller"]TheBand2: [input class="menucontroller" id="bandcb" type="checkbox" ng-model="thebandchecked" /][/labe][ [label class =“ menucontroller”] TheBand2:[input class =“ menucontroller” id =“ bandcb” type =“ checkbox” ng-model =“ thebandchecked” /] [/ labe] [
[label class="menucontroller"]Gallery2: [input class="menucontroller" id="gallerycb" type="checkbox" ng-model="gallerychecked" /][/label][br /] [label class =“ menucontroller”] Gallery2:[input class =“ menucontroller” id =“ gallerycb” type =“ checkbox” ng-model =“ gallerychecked” /] [/ label] [br /]
JavaScript : JavaScript :
window.onload = function () {
}
function homeclicked() {
document.getElementById('homecb').checked = true;
document.getElementById('bandcb').checked = false;
document.getElementById('gallerycb').checked = false;
}
function thebandclicked() {
document.getElementById('homecb').checked = false;
document.getElementById('bandcb').checked = true;
document.getElementById('gallerycb').checked = false;
}
function galleryclicked() {
document.getElementById('homecb').checked = false;
document.getElementById('bandcb').checked = false;
document.getElementById('gallerycb').checked = true
}
you're doing it wrong, ng-if
does not trigger because you only manipulate the checked and unchecked of the check box(using javascript) and not the ng-model, dont try to use javascript to do that, instead do it on your angular controller. 您做错了, ng-if
不会触发,因为您只操作了复选框的选中和未选中(使用javascript),而不是ng-model,请不要尝试使用javascript来做到这一点,而是在您的角度控制器。
Controller: 控制器:
$scope.homeclicked = function() {
$scope.homechecked = true;
$scope.thebandchecked = false;
$scope.gallerychecked = false;
};
ng-model on checkbox inputs handles checked and unchecked of it, also there is a native directive https://docs.angularjs.org/api/ng/directive/ngChecked for handling of check and uncheck. 复选框输入上的ng-model处理它的选中和未选中状态,还有一个本机指令https://docs.angularjs.org/api/ng/directive/ngChecked用于处理选中和取消选中状态。
Do it in proper angular way. 以正确的角度进行操作。
Move the functions inside your controller. 在控制器内部移动功能。 Call them via ng-click
. 通过ng-click
调用它们。 Let the functions change the scope variables which are bound to check boxes. 让函数更改绑定到复选框的范围变量。
<li id="homebutton" ng-if="homechecked != true" ng-click="homeclicked()">
In the mean time inside your scope... 同时,在您的范围内...
$scope.homeclicked = function {
$scope.homechecked = true;
$scope.thebandchecked = false;
$scope.gallerychecked = false;
}
If you really really want to disregard my advice and go ahead with what you have, which is by the way very bad, you can add this line inside your functions: angular.element(document.getElementById('homecb')).$scope().$apply(); 如果您真的很想忽略我的建议并继续进行操作,这很糟糕,可以在函数中添加以下代码:angular.element(document.getElementById('homecb'))。$ scope ()。$ apply(); This is again very bad and should totally be avoided. 这又是非常糟糕的,应该完全避免。
Use 采用
$scope.$apply();
It will run the digest loop and your ng-if will work as u expect. 它将运行摘要循环,并且您的ng-if将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.