Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 为甚么要加上一句;“价值”;归因于一个<;选项>;元素导致选择错误的值?_Angularjs_Angularjs Ng Repeat - Fatal编程技术网

Angularjs 为甚么要加上一句;“价值”;归因于一个<;选项>;元素导致选择错误的值?

Angularjs 为甚么要加上一句;“价值”;归因于一个<;选项>;元素导致选择错误的值?,angularjs,angularjs-ng-repeat,Angularjs,Angularjs Ng Repeat,我使用的是angular 1.2.28,我有一个带有简单值数组的控制器: $scope.transTypes = ['Write-Off', 'Adjustment']; $scope.selectedType = 'Adjustment' 我使用它来填充下拉元素 如果我像这样创建标记(没有“value”属性),则会显示正确的值: <select ng-model="selectedType"> <option ng-repeat="type in transTy

我使用的是angular 1.2.28,我有一个带有简单值数组的控制器:

$scope.transTypes = ['Write-Off', 'Adjustment'];
$scope.selectedType = 'Adjustment'
我使用它来填充
下拉元素

如果我像这样创建标记(没有“value”属性),则会显示正确的值:

  <select ng-model="selectedType">
    <option ng-repeat="type in transTypes">{{type}}</option>
  </select>
  <select ng-model="selectedType">
    <option ng-repeat="type in transTypes" value="{{type}}">{{type}}</option>
  </select>

{{type}}
使用“值”属性创建标记时,会显示错误的值:

  <select ng-model="selectedType">
    <option ng-repeat="type in transTypes">{{type}}</option>
  </select>
  <select ng-model="selectedType">
    <option ng-repeat="type in transTypes" value="{{type}}">{{type}}</option>
  </select>

{{type}}

Plunker:

我尝试升级到angular 1.3的最新版本,但错误依然存在。当我升级到angular 1.4时,bug似乎消失了

第1.4条的工程:

早期版本的angular可能不支持设置“value”属性

ignore this line, can't submit without code snippet.

我尝试升级到angular 1.3的最新版本,但错误依然存在。当我升级到angular 1.4时,bug似乎消失了

第1.4条的工程:

早期版本的angular可能不支持设置“value”属性

ignore this line, can't submit without code snippet.

您需要创建一个控制器来将变量传递给dom。 例如:

HTML:

<html ng-app="App">
  <body ng-controller="myController as myCtrl">

    <select ng-model="myCtrl.selectedType">
      <option ng-repeat="type in myCtrl.transTypes">{{type}}</option>
    </select>

  </body>
</html>
angular.module('App', [])
.controller('myController', function() {

  var _this = this;

  _this.transTypes = ['Write-Off', 'Adjustment'];
  _this.selectedType = 'Adjustment'

});
你可以在行动中看到:

<html ng-app="App">
  <body ng-controller="myController as myCtrl">

    <select ng-model="myCtrl.selectedType">
      <option ng-repeat="type in myCtrl.transTypes">{{type}}</option>
    </select>

  </body>
</html>
angular.module('App', [])
.controller('myController', function() {

  var _this = this;

  _this.transTypes = ['Write-Off', 'Adjustment'];
  _this.selectedType = 'Adjustment'

});

您需要创建一个控制器来将变量传递给dom。 例如:

HTML:

<html ng-app="App">
  <body ng-controller="myController as myCtrl">

    <select ng-model="myCtrl.selectedType">
      <option ng-repeat="type in myCtrl.transTypes">{{type}}</option>
    </select>

  </body>
</html>
angular.module('App', [])
.controller('myController', function() {

  var _this = this;

  _this.transTypes = ['Write-Off', 'Adjustment'];
  _this.selectedType = 'Adjustment'

});
你可以在行动中看到:

<html ng-app="App">
  <body ng-controller="myController as myCtrl">

    <select ng-model="myCtrl.selectedType">
      <option ng-repeat="type in myCtrl.transTypes">{{type}}</option>
    </select>

  </body>
</html>
angular.module('App', [])
.controller('myController', function() {

  var _this = this;

  _this.transTypes = ['Write-Off', 'Adjustment'];
  _this.selectedType = 'Adjustment'

});

我在您的plunker中使用了
ng值,效果很好,angular可能在较新版本中寻找值来修复此类问题。

我在您的plunker中使用了
ng值,效果很好,angular可能在较新版本中查找值以修复此类问题。

只需重命名:
ng value=“type”
。它会起作用的

<select ng-model="selectedType">
            <option  ng-repeat="type in transTypes" ng-value="type">{{type}}</option>
          </select>

{{type}}

只需重命名:
ng value=“type”
。它会起作用的

<select ng-model="selectedType">
            <option  ng-repeat="type in transTypes" ng-value="type">{{type}}</option>
          </select>

{{type}}

在您这样的情况下使用是非常明智的!在你这样的情况下使用是非常明智的!谢谢,看来这就成功了。想知道为什么我在一些html属性上需要ng前缀,而在其他属性上不需要ng前缀?你能给我一个没有ng前缀的例子吗?我可以在一个
上使用高度和宽度,我不必给它们加前缀。难道不应该以同样的方式来评价工作吗?谢谢,看来这就成功了。想知道为什么我在一些html属性上需要ng前缀,而在其他属性上不需要ng前缀?你能给我一个没有ng前缀的例子吗?我可以在一个
上使用高度和宽度,我不必给它们加前缀。难道不应该以同样的方式来评价工作吗?