admin管理员组文章数量:1221459
I seem to cannot get this to work. So I have a bunch of genres of movies which I want them to be checked if those are the genres in user database. This is my code
%section(ng-controller="UserCtrl" ng-init="user_genres=#{preferred_genres}")
%ul
%li(ng:repeat="genre in preferred_genres")
%input(type = "checkbox" ng:model="preferred_genres[genre]" id="genre-{{$index + 1}}" ng-checked="user_genres['{{genre}}']")
%label{:for => "genre-{{$index + 1}}"} {{genre}}
And this is #{preferred_genres}
from haml
{"Action & Adventure":true,"Animation":true,"Art House & International":true,"Classics":true,"Comedy":true,"Documentary":true,"Drama":true,"Horror":true,"Kids & Family":true,"Musical & Performing Arts":true,"Mystery & Suspense":true,"Romance":true,"Science Fiction & Fantasy":true,"Special Interest":true,"Sports & Fitness":true,"Television":true,"Western":true}
So that means every checkbox should be checked. But no checkbox is checked when I load the page. However if I hardcode ng-checked
to be something like this it works.
ng-checked="user_genres['Western']"
This is really strange. Please help.
I seem to cannot get this to work. So I have a bunch of genres of movies which I want them to be checked if those are the genres in user database. This is my code
%section(ng-controller="UserCtrl" ng-init="user_genres=#{preferred_genres}")
%ul
%li(ng:repeat="genre in preferred_genres")
%input(type = "checkbox" ng:model="preferred_genres[genre]" id="genre-{{$index + 1}}" ng-checked="user_genres['{{genre}}']")
%label{:for => "genre-{{$index + 1}}"} {{genre}}
And this is #{preferred_genres}
from haml
{"Action & Adventure":true,"Animation":true,"Art House & International":true,"Classics":true,"Comedy":true,"Documentary":true,"Drama":true,"Horror":true,"Kids & Family":true,"Musical & Performing Arts":true,"Mystery & Suspense":true,"Romance":true,"Science Fiction & Fantasy":true,"Special Interest":true,"Sports & Fitness":true,"Television":true,"Western":true}
So that means every checkbox should be checked. But no checkbox is checked when I load the page. However if I hardcode ng-checked
to be something like this it works.
ng-checked="user_genres['Western']"
This is really strange. Please help.
Share Improve this question asked Jan 8, 2013 at 23:29 toytoy 12.1k28 gold badges100 silver badges180 bronze badges1 Answer
Reset to default 15The value of ng-checked
needs to be an expression. Get rid of the {{}}. So, something like this:
%input(type = "checkbox" ng-model="preferred_genres[genre]" id="genre-{{$index + 1}}" ng-checked="user_genres[genre]")
Also, you don't need to necessarily even have the ng-checked
directive. If the value of your model is true, then it will be checked for you.
本文标签: javascriptHow to preselect checkbox in angularJs with ngcheckedStack Overflow
版权声明:本文标题:javascript - How to pre-select checkbox in angularJs with ng-checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739352708a2159457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论