gpt4 book ai didi

javascript - AngularJS - 动态更改属性值

转载 作者:行者123 更新时间:2023-12-03 06:16:20 27 4
gpt4 key购买 nike

我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com