0

如何使默认情况下禁用文本输入字段并使用复选框切换文本输入字段。我得到的复选框切换启用和禁用状态之间的输入字段,但我不能让输入字段被默认禁用,即使我在加载页面时将复选框设置为true。如何使用角度指令使默认输入禁用

这个方法我试过,但直到我切换复选框和关闭不会禁止输入字段:

<input type="text" ng-disabled="restaurant.valid_shortname" ng-model="restaurant.shortname" > 
<input type="checkbox" ng-model="restaurant.valid_shortname" ng-checked="true"/> 

我希望做的是有复选框选中页面加载时作为reseult从ng-checked的文档禁用输入字段

回答

1

请注意,这个指令不应该连同ngModel使用,因为这可能会导致UNE预期的行为。

正确的方法可能是将您的控制器中的restaurant.valid_shortname设置为true。如果您坚持在您的模板中执行此操作,您也可以使用ng-init。这看起来像这样:

<input ng-init="restaurant.valid_shortname = true" type="text" ng-disabled="restaurant.valid_shortname" ng-model="restaurant.shortname" > 
<input type="checkbox" ng-model="restaurant.valid_shortname"/> 
+0

非常感谢,这正是我所期待的。诚实地避免在我的控制器中做到这一点。完全忘了ng-init。 – Courtney