Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs动态更改模型绑定_Javascript_Angularjs - Fatal编程技术网

Javascript angularjs动态更改模型绑定

Javascript angularjs动态更改模型绑定,javascript,angularjs,Javascript,Angularjs,我有两张表格,一张是账单,一张是发货单。如果用户选中复选框,则发货地址应填充账单地址并禁用。如果用户取消选中该框,则发货地址应为空并返回到启用状态 我现在用$watch做这个,但感觉很粗糙。我有两块$watchs嵌套在一起,看着同一个元素。我想知道是否有更好的方法来实现我正在做的事情 我尝试在ng模型中使用三元运算符,如下所示,但也不起作用 <input ng-model="isSameAsBilling ? billName : shipName" ng-disabled="isSame

我有两张表格,一张是账单,一张是发货单。如果用户选中复选框,则发货地址应填充账单地址并禁用。如果用户取消选中该框,则发货地址应为空并返回到启用状态

我现在用$watch做这个,但感觉很粗糙。我有两块$watchs嵌套在一起,看着同一个元素。我想知道是否有更好的方法来实现我正在做的事情

我尝试在ng模型中使用三元运算符,如下所示,但也不起作用

<input ng-model="isSameAsBilling ? billName : shipName" ng-disabled="isSameAsBilling" />

我想我终于得到你想要的了

选中该复选框后,它会在
billName
上注册$watch,并将其镜像到
shipName

当复选框未选中时,取消注册$watch并清除
shipName

angular

  .module('app', [])

  .controller('appController', ['$scope', function($scope){

    $scope.isSameAsBilling = false;

    $scope.isSameChanged = function() {
      if ($scope.isSameAsBilling) {
        // register the watcher when checked
        $scope.nameWatcher = $scope.$watch('billName', function(bName) {
          $scope.shipName = bName
        })
      } else {
        // deregister the watcher and clear the shipName when unchecked
        $scope.nameWatcher();
        $scope.shipName = ''
      }
    }

  }]);

这是我尝试过的

,但问题是你只能及时捕捉到价值。如果你选中复选框,内容会复制过来,但如果你在billName中键入内容,它将不会反映到shipName中。啊,灯泡。好吧,我明白问题的症结所在。让我想想好吧,你可以用两块手表,但它们不需要嵌套。一个手表在billName上,如果复选框被选中,它会更新shipName,另一个手表在复选框值上,就像我以前一样。
$scope.isSameAsBilling = false;

$scope.sameAsBillingClicked = function(){
  $scope.isSameAsBilling = !$scope.isSameAsBilling;
};

$scope.$watch('isSameAsBilling', function(isSame){

  if ($scope.isSameAsBilling) {

    var shipNameWatcher = $scope.$watch('billName', function (newShipName) {
      $scope.shipName = $scope.billName;

      var secondBillWatcher = $scope.$watch('isSameAsBilling', function(isChecked){
        if (!isChecked){
          shipNameWatcher();
          secondBillWatcher();
          $scope.shipName = '';
        }
      });
    });
  }
});
angular

  .module('app', [])

  .controller('appController', ['$scope', function($scope){

    $scope.isSameAsBilling = false;

    $scope.isSameChanged = function() {
      if ($scope.isSameAsBilling) {
        // register the watcher when checked
        $scope.nameWatcher = $scope.$watch('billName', function(bName) {
          $scope.shipName = bName
        })
      } else {
        // deregister the watcher and clear the shipName when unchecked
        $scope.nameWatcher();
        $scope.shipName = ''
      }
    }

  }]);