1

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

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

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

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>

谢谢 :) 请帮忙

4

1 回答 1

1

您的代码中有几个错误。

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

我更正了它们并更新了代码段。

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>

于 2018-09-04T12:47:14.627 回答