angularjs lib默认值中的某些prbm在select(value和name)中是否有效?

时间:2016-01-13 11:04:09

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-controller

这里我创建了一个用于选择角度的样本...哪个工作正常..我试图设置默认值为select如果我设置这个$ scope.selectedValue =" SureshRaina&#34 ;; 工作得很好......但如果我设置这个$ scope.selectedValue =" Arun&#34 ;;它不起作用......我可以知道这个例子中有什么问题......



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

app.controller('MainCtrl', function($scope, $timeout) {
  $scope.name = 'World';
  
  $timeout(function(){
    $scope.values = [
        { Name: "Arun", Value: "Arun" },
        { Name: "Suresh Raina", Value: "SureshRaina" },
        { Name: "Nelson Raj", Value: "NelsonRaj" }
      ];
  }, 2000);
  
  $scope.selectedValue = "Arun";
  //$scope.selectedValue = "SureshRaina";
 
});

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Sample Application</title>
     <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{selectedValue}}!</p>
    
    <select ng-model="selectedValue" ng-options="val.Value as val.Name for val in values"></select>
    
  </body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您应该在track by val.Value指令中使用select as关键字时使用ng-options

ng-options="val.Value as val.Name for val in values track by val.Value"

Doc link