javascript - 从 Controller 读取 JSON 文件并使用 ng-repeat 显示为 <li>

标签 javascript json angularjs

我的 employee.json 文件与我的 Controller 位于同一目录中。

这是我的 json 文件中的数据:

["John", "Clark"]

在我的 Controller 上:

.controller('EmployeeCtrl', function ($scope, $http) {
  $http.get('employee.json').then(function (res) {
    $scope.contents = res.data;
    });
 });

在我的模板上:

<div>
 <label class="control-label">Identify yourself:</label>
 <select ng-model="market.name">
   <option ng-repeat="content in contents" value="{{content}}">{{content}} </option>
 </select>
</div>

有谁知道为什么这不起作用?我的下拉列表不显示任何内容。

最佳答案

.controller('EmployeeCtrl', function ($scope, $http) {
  $http.get('employee.json').success(function (res) {
    $scope.contents = response.data;
  });
});

还要确保 JSON 文件中的数据格式正确。

关于javascript - 从 Controller 读取 JSON 文件并使用 ng-repeat 显示为 <li>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31817612/

相关文章:

MySQL 实验室 JSON native 类型 : How SUM the result of an array returned by jsn_extract?

AngularJs 参数给所有对象一个 id

javascript - 为什么 setTimeout 阻止递归 $http.get 调用

javascript - SVG getIntersectionList 返回 null

Javascript : string to date

javascript - 在 CKEditor 中修改表单元素

c# - JSON解析C#无名称变量

javascript - Ajax JSON SUMIF 数组包含特定单词

javascript - 仅在通过 $http 加载数据后, Angular 应用程序中的初始化指令

javascript - 如何更改 DataBound 事件中的网格列值