首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS $routeParams /$routeParams不从JSON获取数据

AngularJS $routeParams /$routeParams不从JSON获取数据
EN

Stack Overflow用户
提问于 2016-05-02 12:09:49
回答 1查看 178关注 0票数 0

在测试-细节. curly页面(部分视图)上,不获取/显示来自各自json文件的数据,即在test- TestDetailCtrl .curly中调用控制器(如在工作警报按钮上所看到的那样),但是视图中的params花括号(test-细节.curly)为空。但是,用于test-list.html的ListController工作并获取tests.json。

档案结构:

  • index.html
  • (测试)
    • tests.json (在test-list.html中正确显示的内容)
    • a.json
    • b.json

  • (联合来文)
  • (部分)
  • (css)

partials/test-list.html:

代码语言:javascript
复制
<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-细节.test(其中没有显示任何数据,而且所有的复选标记都是假的):

代码语言:javascript
复制
<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中的路由提供程序:

代码语言:javascript
复制
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'});
}]);

使用js/services.js处理RESTful:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
[{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
    "items": true,
    "duration": false}
}]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-02 12:24:27

我想你把aspx部分和角数组混合在一起了。

角纳克-重复将只在客户端工作,并将评估您的scope.tests数组,当它完成加载。

您的aspx部分(test-细节. will )可能会在服务器端工作,并将依赖于您将其与容器绑定的数据。

编辑:,你的测试-.看起来很奇怪.为什么要用括号来封装呢?(为什么它是一个数组?)partials/test-a.html不是为数组创建的。试试这个JSON (test-a.json):

代码语言:javascript
复制
{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
    "items": true,
    "duration": false}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36982110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档