【发布时间】:2013-08-21 20:48:56
【问题描述】:
无法加载外部 json 文件并将其内容显示在我的视图中。我已经包含了我的视图、控制器和服务代码。我需要改变什么?
view.html
<div ng-controller='BaseCtrl'>
<table class="table table-hover">
<tbody>
<tr class="tr-sep" ng-repeat="example in examples" ng-click="showUser(example)">
<td>{{example.name}}</td>
<td>{{example.type}}</td>
<td>{{example.size}}</td>
</tr>
</tbody>
</table>
</div>
controller.js
'use strict';
angular.module('projyApp')
.controller('BaseCtrl', function ($scope, data) {
$scope.examples = data.getAllExamples();
$scope.showUser = function(example) {
window.location = '#/user/' +example.size;
};
});
service.js
'use strict';
angular.module('projyApp')
.service('data', function data() {
var examples;
var getAllExamples = function () {
$http.get("../../TestData/Examples.json").success($scope.examples = data.examples);
};
});
【问题讨论】:
-
在您的服务中,您需要返回 data.examples 而不是将其分配给 $scope。 $scope 在数据服务中未定义。
-
console: "TypeError: Object # has no method 'getAllObservers' at new
" -
我在您发布的代码 getAllObservers 函数中看不到任何地方。你能做一个 JsFiddle 吗?
-
我的错,getAllExamples 是我要输入的。
-
@captainrad 我刚刚发布了一个答案,解释了我在您的代码中看到的问题。
标签: json angularjs model-view