无法在选择标记

时间:2016-02-23 00:58:07

标签: angularjs json tags

enter image description here

所以我这里有一个角度代码来获取json记录。一切顺利。但它不会显示在视图上。我希望json结果显示在选择标记上,这里是视图部分的代码

<div class="col s6 input-field" ng-controller="positionController">
<select id="selectPosition" ng-model="selectedPosition" ng-options="item as item.PosDesc for item in records">
</select>
<label>Choose Position</label>
</div>

但它并没有在视图上显示任何内容。我的代码有问题吗?谢谢。

编辑:这里是json值 enter image description here

2 个答案:

答案 0 :(得分:1)

您的代码运行正常。

查看此演示。

&#13;
&#13;
(function() {
  var app = angular.module("app", []);

  app.filter("capitalize", function() {
    return function(input, all) {
      var reg = (all) ? /([^\W_]+[^\s-]*) */g : /([^\W_]+[^\s-]*)/;
      return (!!input) ? input.replace(reg, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
      }) : "";
    };
  });

  app.controller("positionController", ["$scope", "$http",
    function($scope, $http) {
      $http.get("https://gist.githubusercontent.com/dannyjhonston/dfbb68cffa626c2e29b3/raw/5c6fc8c1f7529fe25a6a848e9154c5b778dd3eb3/jsonFile.json").success(function(response) {
        $scope.records = response;
        $scope.selectedPosition = $scope.records[0];
      });
    }
  ]);
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="app">
  <div class="col s6 input-field" ng-controller="positionController">
    <select id="selectPosition" ng-model="selectedPosition" ng-options="item as item.PosDesc for item in records">
    </select>
    <label>Choose Position</label>
  </div>
</div>
&#13;
&#13;
&#13;

JSFiddle

答案 1 :(得分:0)

更改ng-model =&#34; selectedPosition&#34;到ng-model =&#34;(selectedPosition | json)&#34;。要在选择中使用角度代码,如:

<select id="selectPosition" ng-model="(selectedPosition | json)" 
   ng-options="item as item.PosDesc for item in records">
   {{ selectedPosition | json }}
</select>