【问题标题】:Possible to dynamically change ng-model source?可以动态更改 ng-model 源吗?
【发布时间】:2015-04-15 11:42:31
【问题描述】:

当输入未显示时,是否可以将输入“取消绑定”到范围变量?

EG:

<input type="text" ng-model="value1" ng-show="true">
//The above input value would bind to $scope.value1


<input type="text" ng-model="value2" ng-show="false">
//The above input value would NOT bind to $scope.value2 as its hidden

Pheraps 有一种方法可以在 ng-model 中使用 if 语句

EG:

<input type="text" ng-model="isBinded ? value : ''" ng-init="isBinded = true">
//The above input value is binds to $scope.value according to isBinded true or false

【问题讨论】:

  • 使用 ng-if 代替 ng-hide 和 ng-show..

标签: javascript angularjs data-binding angular-ngmodel


【解决方案1】:

您可以使用不同的ng-model 创建两个输入标签,并在这些输入标签上使用ng-hide or ng-show

【讨论】:

    【解决方案2】:

    在网页上:

    <button ng-click="booleanValue1 = !booleanValue1">Toggle Show</button>
    
    <input type="text" ng-model="value1show" ng-show="booleanValue1">
    

    在控制器上:

    $scope.value1 = "hello";
    
    $scope.value1show = $scope.value1false = $scope.value1;
    
    $scope.booleanValue1 = true;
    
    $scope.$watch('booleanValue1', function () {
        if ($scope.booleanValue1) {
           $scope.value1 = $scope.value1show;
        } else {
           $scope.value1 = $scope.value1false;
        }
        console.log("value1 " + $scope.value1);
        console.log("value1show " + $scope.value1show);
        console.log("value1false " + $scope.value1false);
        console.log("");
    });
    

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2013-09-09
      • 2015-12-01
      • 2016-02-28
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多