AngularJS problems with ng-options selected value

倾然丶 夕夏残阳落幕 提交于 2019-12-01 22:49:15

问题


I'm facing a strange problem when using ng-options with AngularJS.

My scenario is pretty straight forward:

  1. Bind a value with ng-model to be the selected option
  2. Load the values of the 'select' from a backend
  3. Bind the loaded values to the 'select'

My objects loaded from the backend is a key/value like:

{
   Value: "my_value",
   Name: "my_name"
}

And everything works fine... Until Name and Value are the same. Then angular won't bind the selected value correct anymore.

I have created this plunker: https://plnkr.co/edit/tUBXIpMTBAHK2Xh8aDu6?p=preview To demonstrate the problem.

My controller:

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

app.controller('MainCtrl', function($scope, $timeout) {
  $scope.name = 'World';

  $timeout(function(){
    $scope.values = [
        { Name: "Accepted", Value: "Accepted" },
        { Name: "Not accepted", Value: "NotAccepted" },
        { Name: "Not at all accepted", Value: "NotAtAllAccepted" }
      ];
  }, 2000);

  $scope.selectedValue = "Accepted";
  //$scope.selectedValue = "NotAccepted";
  //$scope.selectedValue = "NotAtAllAccepted";
});

If the selected value in the plunker example is set to 'Accepted' it will not work. But if the selected value is set to some of the other values - then it works fine.

And my HTML:

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

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{selectedValue}}!</p>

    <select ng-model="selectedValue" ng-options="orderStatus.Value as orderStatus.Name for orderStatus in values"></select>

  </body>

</html>

Anyone who can enlighten me on this one?

Thanks!

UPDATE

It looks like it works fine in Firefox. But in Chrome and IE the problem still occurres.


回答1:


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

app.controller('MainCtrl', function($scope, $timeout) {
  $scope.name = 'World';
  
  $timeout(function(){
    $scope.values = [
        { 'Name': "Accepted", 'Value': "Accepted" },
        { 'Name': "Not accepted", 'Value': "NotAccepted" },
        {'Name': "Not at all accepted", 'Value': "NotAtAllAccepted" }
      ];
    $scope.selectedValue = "Accepted";
  }, 2000);
  
  
  //$scope.selectedValue = "NotAccepted";
  //$scope.selectedValue = "NotAtAllAccepted";
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
    <script src="app.js"></script>
  </head>

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

</html>


来源:https://stackoverflow.com/questions/34742678/angularjs-problems-with-ng-options-selected-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!