【问题标题】:ngModel remove the Checked status of checkbox automaticallyngModel 自动删除复选框的选中状态
【发布时间】:2016-12-04 03:27:19
【问题描述】:

我正在使用 Angular 来验证复选框并为此使用 ng-model

<input type="checkbox" name="news1" value="news1" ng-model="news" <c:if test="${xxxx == yes'}">checked="checked"></c:if>>
<label ng-click="news1();"></label>

当通过传递 news1 状态的链接调用嵌入复选框的页面时(是).. 我的意思是用户是否已经注册 news1。如果是这样,则应动态检查该复选框,否则未选中。

问题是当我在复选框中使用/添加:ng-model="news1" 作为属性时,它无法正常工作。复选框被选中了几秒钟,它突然变得未选中。

在控制器中,我有以下功能可以在页面加载后更改复选框的状态,以便用户可以选中和/或取消选中复选框:

$scope.news1 = function (){
    if(!$scope.news1){
        $scope.news1 = !$scope.news1;
    } else{
        $scope.news1 = !$scope.news1;
    }
};

问题是,如何解决此问题,以便当链接中 news1 的状态为:是时,复选框不断被选中。 如何防止 Angular 自动将复选框设置为未选中?

谢谢!

【问题讨论】:

  • 您对属性和函数名称使用相同的名称。重命名两者以更具描述性,这将解决您的问题。
  • 使用 value 或 ng-model,而不是两者。
  • 它们都没有帮助...当我检查 chrome 中生成的代码时,我看到 angular 在属性类 calss="check-filialen ng-pristine ng 中添加以下复选框作为值-untouched ng-valid ng-empty"

标签: javascript angularjs checkbox


【解决方案1】:

我找到了解决方案。它是ng-init="xxxxx=true"

<input type="checkbox" name="news1" value="news1" ng-model="news" <c:if test="${xxxx == yes'}"> ng-init="news=true"></c:if>>

谢谢!

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多