可以动态更改ng-model源?

时间:2015-04-15 11:42:32

标签: javascript angularjs data-binding angular-ngmodel

是否有可能解开'输入未显示时输入到范围变量?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在网页上:

<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("");
});