javascript - AngularJS - 动态更改属性值

标签 javascript angularjs

我正在使用 angularjs 应用程序,我想动态设置它的其中一个属性。

这是我的模型的一部分:

    $scope.data_step_3 = {
        'previous_insurance':{
            'label':'Has this type of insurance ever been:',
            'help_text':'',
            'value':'',
            'required':true,
            'regex':''
        },
        'previous_insurance_description':{
            'label':'If so, please explain (not applicable in Missouri):',
            'help_text':'',
            'value':'',
            'required': true,
            'regex':''
        }
}

我会动态设置 previous_insurance_descriptionrequired 属性。当 previous_insurance.value == 'Non-reviewed'

时,它会是 true

我怎样才能做到这一点?

感谢您的帮助

最佳答案

要达到预期结果,请使用以下选项

HTML:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="x in data_step_3">
 <input value="" ng-model="x.value" ng-keyup="check()">
    <input value="" ng-model="x.label"><br>

  </div>
<span>Required value - {{data_step_3.previous_insurance_description.required}}</span>
</div>



</body>
</html>

JS:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.data_step_3 = {
        'previous_insurance':{
            'label':'Has this type of insurance ever been:',
            'help_text':'',
            'value':'',
            'required':true,
            'regex':''
        },
        'previous_insurance_description':{
            'label':'If so, please explain (not applicable in Missouri):',
            'help_text':'',
            'value':'',
            'required': true,
            'regex':''
        }
}

    $scope.check=function(){
     var prevInsurance_val= $scope.data_step_3.previous_insurance.value;
      console.log(prevInsurance_val)
     if(prevInsurance_val =="Non-reviewed"){
       $scope.data_step_3.previous_insurance_description.required = false;
      }
      else{
         $scope.data_step_3.previous_insurance_description.required = true;
      }
    }
    console.log()
});

Codepen-http://codepen.io/nagasai/pen/YWbxpA

关于javascript - AngularJS - 动态更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39088020/

相关文章:

javascript - 使用配置文件时的前缀问题

javascript - 我想使用带有 ng-repeat 的单选按钮,但它不起作用

javascript - AngularFire:返回 $value

javascript - ngSrc 在 ngClick 上更新,但在 keydown 事件处理程序上不更新

angularjs - Selenium 并行测试在多个浏览器实例之间运行冲突 b/c 共享本地存储?

angularjs - Angular JS 使用 $http() 获得响应,状态为 200,但数据为 null

javascript - Shiny 表格中数学模式的外部 js

javascript - fullpage.js 中水平导航栏上带有点的标题

javascript - 从 Javascript 中的文件读取

javascript - 如何在js代码中为vb变量添加值?