AngularJS $routeParams / 服务未从 JSON 获取数据
AngularJS $routeParams / service not fetching data from JSON
在测试-detail.html页面(部分视图)中,来自各个json文件的数据不是fetched/displayed,即在测试-detail.html中调用了控制器TestDetailCtrl (可以在工作警报按钮上看到),但是视图中的参数大括号 (test-detail.html) 是空的。
然而,用于 test-list.html 的 ListController 工作并获取 tests.json。
文件结构:
- index.html
- (测试)
- tests.json(测试中正确显示的内容-list.html)
- a.json
- b.json
- (js)
- (部分)
- (css)
partials/test-list.html:
<form action="" data-ng-repeat="test in tests | orderBy:orderProp">
<input type="checkbox" data-ng-model="item.selected" data-ng-change="change(item)">
<a href="#/tests/{{test.id}}">
<img data-ng-src="{{test.imageUrl}}" alt="{{test.name}}" class="test-thumb">
</a>
<a href="#/tests/{{test.id}}">
{{ test.name }}
</a><br />
</form>
partials/test-detail.html(其中 none 的数据被显示并且所有勾号都是错误的):
<div class="main">
<img data-ng-src="{{mainImageUrl}}"/>
<h2>{{ test.name }}</h2>
<p>{{ test.description }}</p>
Customization:
<ul>
<li>Test items: {{test.customization.items | checkmark }}</li>
<li>Test duration: {{test.customization.duration | checkmark }}</li>
</ul>
</div>
js/app.js 中的路由提供者:
app.config(['$routeProvider',
{$routeProvider
.when('/tests',
{templateUrl: 'partials/test-list.html', controller: 'ListController'})
.when('/tests/:testId',
{templateUrl: 'partials/test-detail.html', controller: 'TestDetailCtrl'})
.otherwise({redirectTo: '/tests'});
}]);
RESTful 使用 js/services.js 处理:
var appServices;
appServices = angular.module('appServices', ['ngResource']);
appServices.factory('Test', ['$resource',
function($resource){
return $resource('tests/:testId.json', {}, {
query: {method:'GET', params:{testId:'tests'}, isArray:true}
});
}]);
controllers.js:
var ctr = angular.module('myApp.controller', []);
ctr.controller('ListController', ['$scope', 'Test', function ($scope, Test)
{$scope.tests = Test.query(); /*works*/
$scope.orderProp = 'duration';}]);
ctr.controller('TestDetailCtrl', ['$scope', '$routeParams', 'Test', function($scope, $routeParams, Test)
{$scope.$on('$routeChangeSuccess', function() {
$scope.test = Test.get({testId: $routeParams.testId}, function(test) {
$scope.mainImageUrl = test.screenshots[0];
});
$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
};
$scope.hello = function(name) {
alert('Test ' + (name || 'works' + '!')); /*works*/
}
})}
]);
示例测试-a.json:
[{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
"items": true,
"duration": false}
}]
我认为您在这里将 aspx 部分与 angular 数组混合在一起。
Angular ng-repeat 将仅在客户端工作,并在完成加载后评估您的 scope.tests 数组。
您的 aspx 部分 (test-detail.html) 可能会在服务器端工作,并且取决于您将它与容器进行数据绑定。
编辑: 你的测试-a.json 看起来很奇怪...为什么用括号封装? (为什么是数组?) partials/test-a.html 不是为数组设计的。试试这个 JSON (test-a.json):
{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
"items": true,
"duration": false}
}
在测试-detail.html页面(部分视图)中,来自各个json文件的数据不是fetched/displayed,即在测试-detail.html中调用了控制器TestDetailCtrl (可以在工作警报按钮上看到),但是视图中的参数大括号 (test-detail.html) 是空的。 然而,用于 test-list.html 的 ListController 工作并获取 tests.json。
文件结构:
- index.html
- (测试)
- tests.json(测试中正确显示的内容-list.html)
- a.json
- b.json
- (js)
- (部分)
- (css)
partials/test-list.html:
<form action="" data-ng-repeat="test in tests | orderBy:orderProp">
<input type="checkbox" data-ng-model="item.selected" data-ng-change="change(item)">
<a href="#/tests/{{test.id}}">
<img data-ng-src="{{test.imageUrl}}" alt="{{test.name}}" class="test-thumb">
</a>
<a href="#/tests/{{test.id}}">
{{ test.name }}
</a><br />
</form>
partials/test-detail.html(其中 none 的数据被显示并且所有勾号都是错误的):
<div class="main">
<img data-ng-src="{{mainImageUrl}}"/>
<h2>{{ test.name }}</h2>
<p>{{ test.description }}</p>
Customization:
<ul>
<li>Test items: {{test.customization.items | checkmark }}</li>
<li>Test duration: {{test.customization.duration | checkmark }}</li>
</ul>
</div>
js/app.js 中的路由提供者:
app.config(['$routeProvider',
{$routeProvider
.when('/tests',
{templateUrl: 'partials/test-list.html', controller: 'ListController'})
.when('/tests/:testId',
{templateUrl: 'partials/test-detail.html', controller: 'TestDetailCtrl'})
.otherwise({redirectTo: '/tests'});
}]);
RESTful 使用 js/services.js 处理:
var appServices;
appServices = angular.module('appServices', ['ngResource']);
appServices.factory('Test', ['$resource',
function($resource){
return $resource('tests/:testId.json', {}, {
query: {method:'GET', params:{testId:'tests'}, isArray:true}
});
}]);
controllers.js:
var ctr = angular.module('myApp.controller', []);
ctr.controller('ListController', ['$scope', 'Test', function ($scope, Test)
{$scope.tests = Test.query(); /*works*/
$scope.orderProp = 'duration';}]);
ctr.controller('TestDetailCtrl', ['$scope', '$routeParams', 'Test', function($scope, $routeParams, Test)
{$scope.$on('$routeChangeSuccess', function() {
$scope.test = Test.get({testId: $routeParams.testId}, function(test) {
$scope.mainImageUrl = test.screenshots[0];
});
$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
};
$scope.hello = function(name) {
alert('Test ' + (name || 'works' + '!')); /*works*/
}
})}
]);
示例测试-a.json:
[{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
"items": true,
"duration": false}
}]
我认为您在这里将 aspx 部分与 angular 数组混合在一起。
Angular ng-repeat 将仅在客户端工作,并在完成加载后评估您的 scope.tests 数组。
您的 aspx 部分 (test-detail.html) 可能会在服务器端工作,并且取决于您将它与容器进行数据绑定。
编辑: 你的测试-a.json 看起来很奇怪...为什么用括号封装? (为什么是数组?) partials/test-a.html 不是为数组设计的。试试这个 JSON (test-a.json):
{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
"items": true,
"duration": false}
}