AngularJS小测试,请帮我找错

时间:2014-11-09 11:50:22

标签: javascript angularjs

http://codepen.io/rcidaleassumpo/pen/ogvoQy

您好,代码在上面的网站上。

这就是HTML

  <div ng-app="testApp" ng-controller="myController">
  <select name="current" id="current" ng-model="currentliga">
      <option ng-repeat="liga in ligas">{{ liga }}</option>
  </select>

  {{ valor | currency:"R$" }}
  </div>

这就是JS:

  var app = angular.module('testApp', []);

  app.controller('myController', function($scope){
     $scope.ligas = ['Bronze', 'Prata', 'Ouro', 'Platina', 'Diamante'];
     var preco = $scope.currentliga;
      switch(preco){
        case 'Bronze':
          $scope.valor = 24;
          break;
      }
   });

所以基本上我的想法是从$ scope.currentLiga中获取值,该值与该人将作为选项设置的内容相关联,然后返回&#34; preco&#34;的值。这将改变&#34;勇气&#34; 所以,如果它的青铜&#34;人们选择的,$ scope.valor应该更改为24。 我错过了什么? 谢谢

1 个答案:

答案 0 :(得分:0)

使用$watch

&#13;
&#13;
var app = angular.module('testApp', []);

app.controller('myController', function($scope) {
  $scope.ligas = ['Bronze', 'Prata', 'Ouro', 'Platina', 'Diamante'];

  $scope.$watch('currentliga', function(newValue, oldValue) {
    switch (newValue) {
      case 'Bronze':
        $scope.valor = 24;
        break;
    }
  });


});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="testApp" ng-controller="myController">
  <select name="current" id="current" ng-model="currentliga">
    <option ng-repeat="liga in ligas">{{ liga }}</option>
  </select>

  {{ valor | currency:"R$" }}
</div>
&#13;
&#13;
&#13;