如果AngularJS中的ng-if parent为false,如何将模型设置为null

时间:2019-01-15 19:49:40

标签: javascript html angularjs

如果父级的ng-if为false,我需要ng-models为空

<label><input type="radio" ng-value="true" name="radio1" ng-model="response.answer1" /> Yes</label>
        <label class="margin-radio"><input type="radio" ng-value="false" name="radio1" ng-model="response.answer1" /> No</label>

<div ng-if="response.answer1">
        <input type="text" ng-model="response.answer2" />
</div>

当用户选择“是”时,出现文本输入,然后用户可以输入一些内容。如果他这样做,然后单击“否”,则文本输入将消失,但response.answer2中的值仍然保留,因此如果控件不存在,则希望将其设置为null(ng-if =“ false”)。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用ng-change指令

1.25

在控制器中:

    do

         sub lvl
            level% = 1  
            levelup% = 50  
            experience% = 5 
            ExperTotal% = 0 

        if Expertotal% = levelup% then 'something here to add 1 to level%
while Expertotal% = levelup% 

    ' multiply them both by 1.25

wend
    loop

    end sub

答案 1 :(得分:0)

要获得预期的结果,请使用ng-init分配null

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<label><input type="radio" ng-value="true" name="radio1" ng-model="response.answer1" /> Yes</label>
        <label class="margin-radio"><input type="radio" ng-value="false" name="radio1" ng-model="response.answer1" /> No</label>

<div ng-if="response.answer1">
        <input type="text" ng-model="response.answer2" ng-init="response.answer2=null"/>
</div>

</div>



</body>
</html>

codepen-https://codepen.io/nagasai/pen/ZVPJqJ