ng-checked不更新ng-show

时间:2015-02-19 17:19:35

标签: angularjs

在表单中,我有一个复选框类型输入,如果选中它,则应显示另一个部分。

<input type="checkbox" ng-model="checkedSeries" ng-checked="expression"> This is part of a series.
<div ng-show="checkedSeries">
     <input type="text"name="inputSeriesTitle" placeholder="Some Title Here">
</div>

当我手动点击复选框输入时,它会起作用,显示字段。

但是当使用ng-checked =&#34; expressionIsTrue&#34;自动选中复选框输入后,模型似乎不会更新,并且不显示额外部分

我想我已经从AngularJS文档中正确地完成了这个:

2 个答案:

答案 0 :(得分:1)

ng-modelng-checked没有,也不打算在一起玩得很好。当您只想获得表达式结果的视觉反馈时,可以使用ng-checked,当您关心变量的值时,可以使用ng-model

听起来你想要做的更像是:

<input type="checkbox" ng-model="checkedSeries" ng-init="checkedSeries = expression">
This is part of a series.
<div ng-show="checkedSeries">
     <input type="text"name="inputSeriesTitle" placeholder="Some Title Here">
</div>

答案 1 :(得分:1)

根据角度源代码:

// boolean attrs are evaluated forEach(BOOLEAN_ATTR, function(propName, attrName) { // binding to multiple is not supported if (propName == "multiple") return; function defaultLinkFn(scope, element, attr) { scope.$watch(attr[normalized], function ngBooleanAttrWatchAction(value) { attr.$set(attrName, !!value); }); } var normalized = directiveNormalize('ng-' + attrName); var linkFn = defaultLinkFn; if (propName === 'checked') { linkFn = function(scope, element, attr) { // ensuring ngChecked doesn't interfere with ngModel when both are set on the same input if (attr.ngModel !== attr[normalized]) { defaultLinkFn(scope, element, attr); } }; }

将ng-checked和ng-model设置在一起有效,但它不会影响ng-model。

因此,如果您想在启动时使用ng-model和默认值,则可以设置控制器内复选框的值:

$scope.checkedSeries = true;

相关问题