【问题标题】:NgSelect not selecting the Option in selectNgSelect 没有在选择中选择选项
【发布时间】:2019-02-09 11:29:48
【问题描述】:

即使比较是正确的,Ng-Selected 也没有选择正确的选项。我可以在输出中看到比较工作得很好。

我的实际问题是,即使在成功比较之后(与其他 Int 和字符串比较返回 false 的问题不同)。为什么它不选择该选项。

我不能将选项用于其他目的。其次,在复制到 sn-p 时,它有一些错误。这就是为什么它不起作用。

var app = angular.module("app", []);
app.controller("HelloController", function($scope) {

  $scope.data = {
    ExpertiseId: null,
    userExperties = [{
      id: 1,
      ExpertyTitle: "Human Resource"
    }, {
      id: 2,
      ExpertyTitle: "Account & Finance"
    }, {
      id: 3,
      ExpertyTitle: "Information Technology"
    }, {
      id: 4,
      ExpertyTitle: "Business Management"
    }];
  }
});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>AngularJS</title>

</head>

<body ng-app="app">
  <select name="ChooseExpertise" id="ChooseExpertise" class="form-control" ng-model="newAdmin.ExpertiseId" required>
    <option style="display:none" value="">CHOOSE_EXPERTISE</option> 
    <option ng-selected="{{option.id.toString() == data.ExpertiseId.toString()}}" value="{{option.id.toString()}}" ng-repeat="option in data.userExperties">{{option.id.toString()==data.ExpertiseId.toString()}}---{{option.ExpertyTitle}}</option>
  </select>
</body>

</html>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

谢谢 :) 请帮忙

【问题讨论】:

    标签: javascript angular select dropdown angular-ngselect


    【解决方案1】:

    您的代码中有几个错误。

    1. 对象符号错误
    2. 你没用过 ng-controller
    3. 将脚本文件放在 html 标记之外。
    4. ng-selected 语法也是错误的

    我更正了它们并更新了 sn-p。

    var app = angular.module("app", []);
    app.controller("HelloController", function($scope) {
    
      $scope.data = {
        ExpertiseId: 2,
        userExperties : [{
          id: 1,
          ExpertyTitle: "Human Resource"
        }, {
          id: 2,
          ExpertyTitle: "Account & Finance"
        }, {
          id: 3,
          ExpertyTitle: "Information Technology"
        }, {
          id: 4,
          ExpertyTitle: "Business Management"
        }]
      }
    });
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <title>AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
    </head>
    
    <body ng-app="app" >
    <div ng-controller="HelloController">
      <select name="ChooseExpertise" id="ChooseExpertise" class="form-control" ng-model="newAdmin.ExpertiseId" required>
        <option  ng-selected="option.id === data.ExpertiseId" ng-repeat="option in data.userExperties" value="{{option.id}}">{{option.id}}</option>
      </select>
      </div>
    </body>
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-01
      • 2013-09-14
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多