ng-model 逻辑问题

ng-model issue on logic

我正在处理的 ng-model 有点问题。

首先我将展示 ng-model 正常工作的部分

  <input type="text"
         ng-model="slip.risk"
         ng-change="riskWinCalculations(slip, 'RISKSTRAIGHT')">

  <input type="text"
         ng-model="slip.win"
         ng-change="riskWinCalculations(slip, 'WINSTRAIGHT')">

  <strong>To Win: </strong>{{straightTotalWin | currency}}

  <strong>Total Stake: </strong>{{straightTotalStake | currency}}

现在 html ng-model 对我不起作用

  <input type="text"
         ng-change="riskWinCalculations(null, 'RISKPARLAY')"
         ng-model="riskParlay">

  <strong>To Win: </strong>{{winParlay | currency}}

  <strong>Total Stake: </strong>{{riskParlay | currency}}

在此控制器中,您将看到一个名为 applyParlayRisk 的函数给我带来了问题,而另一个名为 riskWinCalculations 的函数也给我带来了问题 RISKPARLAY,其余的他们,工作起来很有魅力

   applyStraightRisk = function(slip) {
      var moneyLine = _.result(_.find(slip.lines, {isSelected: '1'}), 'moneyLine');
      slip.win = RiskWikCalculations.calculateStraightBet(slip.risk, parseFloat(moneyLine, 10), true);
    }, 

   applyStraightWin = function(slip) {
      var moneyLine = _.result(_.find(slip.lines, {isSelected: '1'}), 'moneyLine');
      slip.risk = RiskWikCalculations.calculateStraightBet(slip.win, parseFloat(moneyLine, 10), false);
    }, 

   applyParlayRisk = function(parlay, riskParlay) {
      $scope.riskParlay = riskParlay;
      console.log($scope.riskParlay);
      //this logs UNDEFINED
      $scope.winParlay = riskParlay * (parlay.payoutProportion || 4.5);
    }, 

  sumStraightStakes = function(betSlip) {
      var totalStake = 0,
          totalWin = 0;
      _.each(betSlip, function(slip) {
        if (slip.risk) {
          totalStake += parseFloat(slip.risk, 10);
        }
        if (slip.win) {
          totalWin += parseFloat(slip.win, 10);
        }
      });
      $scope.straightTotalStake = totalStake;
      $scope.straightTotalWin = totalWin;
    };


    $scope.riskAll = 0;
    $scope.winAll = 0;
    $scope.riskWinCalculations = function(slip, type) {
    switch (type) {
      case 'RISKSTRAIGHT':
        applyStraightRisk(slip, slip.risk);
        break;
      case 'WINSTRAIGHT':
        applyStraightWin(slip, slip.win);
        break;
      case 'RISKPARLAY':
        applyParlayRisk($scope.currentParlay, $scope.riskParlay);
        break;
     }
     sumStraightStakes($scope.betSlip);
    };

所以你认为我哪里错了?

更新

在移动应用程序中,我正在执行完全相同的功能并且工作正常,看

html

  <button ng-click="openRiskWinKeypad(null, 'RISKPARLAY')">
     Risk: {{riskParlay}}
  </button>

  <strong>Total Stake: </strong>{{riskParlay | currency}}

  <strong>To Win: </strong>{{winParlay | currency}}

控制器

applyParlayRisk = function(parlay, amount) {
      $scope.riskParlay = amount;
      $scope.winParlay = amount * (parlay.payoutProportion || 4.5);}

这里有点不同,因为我打开的是自定义键盘

$scope.openRiskWinKeypad = function(slip, type) {
      $scope.keypad = {value: 0};
      if (type === 'RISKSTRAIGHT' && slip.risk) {
        $scope.keypad.value = slip.risk;
      }
      if (type === 'WINSTRAIGHT' && slip.win) {
        $scope.keypad.value = slip.win;
      }

      $ionicPopup.show({
         // here is where the keypad opens
      }).then(function(amount) {
        if (amount) {
          switch (type) {
            case 'RISKSTRAIGHT':
              applyStraightRisk(slip, amount);
              break;
            case 'WINSTRAIGHT':
              applyStraightWin(slip, amount);
              break;
              break;
            case 'RISKPARLAY':
              applyParlayRisk($scope.currentParlay, amount);
              break;
          }
        }
        sumStraightStakes($scope.betSlip);
      });
    }; 

编辑

如果我这样做

<input type="text" ng-model="riskParlay"
       ng-change="riskWinCalculations(null, 'RISKPARLAY')">

applyParlayRisk = function(parlay, riskParlay) {
      $scope.riskParlay = riskParlay;
      console.log($scope.riskParlay);
      $scope.winParlay = riskParlay * (parlay.payoutProportion || 4.5);
    }

    case 'RISKPARLAY':
      applyParlayRisk($scope.currentParlay, riskParlay);
      console.log($scope.currentParlay.name);
      break;

我得到的只是

ReferenceError: riskParlay is not defined

我不能确定,因为你没有 post 整个 html 片段,但我猜 riskParlay 输入和控制器之间还有另一个范围。如果存在,则 ng-model 指令会将 riskParlay 变量写入该范围,而不是控制器的范围。

您的其他输入恰好有效,因为它们的表达式(. 运算符)中有对象索引。带有该运算符的 Ng-model 表达式会查找作用域层次结构,直到它们找到具有被索引对象的作用域。这个视频解释得更好:https://thinkster.io/egghead/the-dot