当ng-hide为真时,清除输入ng模型AngularJS

前端之家收集整理的这篇文章主要介绍了当ng-hide为真时,清除输入ng模型AngularJS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试设置此< input>以某种方式,当ng-hide为true时,重置ng-model中的任何内容.

<input type="text" class="form-control" ng-model="account.AccountName" ng-hide="account.AccountStatus == 'Terminated'">

因此,将有一个包含Active和Terminated绑定到ng-model =“account.AccountStatus”的下拉列表.当它处于活动状态时,字段帐户名称将在那里有文本.我想说,当我切换到Terminated时,该输入字段被隐藏,而account.AccountName被设置为“”.

解决方法

如果AccountStatus等于’Terminated’,您可以使用控制器中的[ng-change]属性和定义函数来处理更改事件并重置AccountName.
例:

<input type="text" class="form-control" ng-model="account.AccountName" ng-hide="account.AccountStatus == 'Terminated'">
    <select ng-change="selectChange()" name="selectState" ng-model="account.AccountStatus">
      <option value="Active">Active</option>
      <option value="Terminated">Terminated</option>
    </select>
    <script>
      angular.module('examleModule',[]).controller('exampleController',['$scope',function($scope) {
         $scope.account = {
            AccountStatus: 'Active',AccountName: ''
         };

     $scope.selectChange= function() {
        // Reset AccountName if AccountStatus=='Terminated'
        if($scope.account.AccountStatus=='Terminated')
             $scope.account.AccountName = '';      
     };
   }]);
   </script>

猜你在找的Angularjs相关文章