不确定为什么它没有约束力

时间:2017-05-30 11:02:47

标签: angularjs

我正在尝试绑定值,但它没有用Even和Odd更新。我正在从数字框更新num1和num2。感谢

var test = angular.module("test", []);

test.controller("testCtrl", function($scope){
  $scope.num1 = 0;
  $scope.num2 = 0;
  $scope.value = ($scope.num1 + $scope.num2) % 2 ? "Even" : "Odd" ;
});
<!DOCTYPE html>
<html ng-app="test">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<body ng-controller="testCtrl">
  First Number : <input type="number" ng-model="num1"></input>
  <br/>
  <br/>
  Second Number : <input type="number" ng-model="num2"></input>
  <br/>
Sum : {{num1 + num2}} is <span>{{value}} </span> <br/>


<br/>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要使用函数触发它并绑定函数

<强>样本

var test = angular.module("test", []);

test.controller("testCtrl", function($scope){
  $scope.num1 = 0;
  $scope.num2 = 0;
  $scope.update = function(){  
     return ($scope.num1 + $scope.num2) % 2 ? "Even" : "Odd" ;
   }
});
<!DOCTYPE html>
<html ng-app="test">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<body ng-controller="testCtrl">
  First Number : <input type="number" ng-model="num1"/>
  <br/>
  <br/>
  Second Number : <input type="number" ng-model="num2"/>
  <br/>
Sum : {{num1 + num2}} is <span>{{update()}} </span> <br/>

</body>
</html>