AngularJS: ngChecked not working
AngularJS: ngChecked not working
我有以下单选按钮:
<input type="radio" ng-value="option.text" ng-model="selected.value" name="option" ng-checked="poll_post.poll_option_id == option.id">
我已经仔细检查了poll_post.poll_option_id
和option.id
是否相等。因此,ng-checked内部的表达式被求值为true。但在视图中,该选项未被选中。我做错了什么?
您不应该同时使用ngModel和ngChecked
ngChecked是一个单向绑定,也就是说,它只会检查表达式的值并相应地设置输入,但如果您更改输入的值,它不会将值传递给绑定到的变量。
另一方面,ngModel将检查其绑定到的变量的值,并在输入发生变化时设置变量的值
ngChecked相对于ngModel的优势在于,可以使用表达式而不是变量。
尽管如此,如果两者都存在,ngModel"战胜"ngChecked(ngChecked的优先级为100,而ngModel的优先级为1,请在此处搜索"优先级":https://docs.angularjs.org/api/ng/service/$compile),并且ngChecked表达式的更改不会传递给变量ngModel绑定到
您可以在这个Plunker中看到,即使Val1
有checked="true"
,变量$scope.selected
也设置为Val2
,因此ng模型战胜了检查的nghttp://plnkr.co/edit/nae3b46KNzFKVTqEQPG3?p=preview
如果你想以编程方式更改复选框的状态,你应该编辑你将ng模型绑定到的变量,或者删除ng模型,只选中ng(这取决于你在做什么)。
有关详细信息:
AngularJS:ng模型未绑定到选中复选框的ng
https://docs.angularjs.org/api/ng/directive/ngChecked
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working