繁体   English   中英

如何使用ng-checked预先选择angularJs中的复选框

[英]How to pre-select checkbox in angularJs with ng-checked

我似乎无法让这个工作。 所以我有很多类型的电影,如果那些是用户数据库中的类型,我希望它们被检查。 这是我的代码


%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}}

这是来自haml的#{preferred_genres}


{"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}

这意味着应该检查每个复选框。 但是,当我加载页面时,没有选中复选框。 但是,如果我硬编码ng-checked是这样的,它的工作原理。


ng-checked="user_genres['Western']"

这真的很奇怪。 请帮忙。

ng-checked的值必须是表达式。 摆脱{{}}。 所以,像这样:

 %input(type = "checkbox" ng-model="preferred_genres[genre]" id="genre-{{$index + 1}}" ng-checked="user_genres[genre]")

此外,您甚至不需要具有ng-checked指令。 如果您的模型的值为true,那么将为您检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM