【问题标题】:AngularJS $routeParams / service not fetching data from JSONAngularJS $routeParams / 服务未从 JSON 获取数据
【发布时间】:2016-05-02 12:09:49
【问题描述】:

在 test-detail.html 页面(部分视图)上,来自各个 json 文件的数据不会被提取/显示,即控制器 TestDetailCtrl 在 test-detail.html 中被调用(可以在工作警报按钮上看到),但视图中的参数大括号(test-detail.html)为空。 但是 test-list.html 的 ListController 可以工作并获取 tests.json。

文件结构:

  • index.html
  • (测试)
    • tests.json(内容正确显示在test-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(没有显示任何数据且所有复选标记均为假):

<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'});
}]);

使用 js/services.js 进行 RESTful 处理:

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*/
    }
   })}
 ]);

示例 test-a.json:

[{
id: "test-a",
name: "test a",
description: "text ... bla",
"customization": {
    "items": true,
    "duration": false}
}]

【问题讨论】:

    标签: angularjs json route-provider routeparams


    【解决方案1】:

    我认为您在这里将 aspx 部分与角度数组混合在一起。

    Angular ng-repeat 仅在客户端工作,并在完成加载时评估您的 scope.tests 数组。

    您的 aspx 部分 (test-detail.html) 可能会在服务器端工作,并且取决于您将其与容器进行数据绑定。

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

    {
    id: "test-a",
    name: "test a",
    description: "text ... bla",
    "customization": {
        "items": true,
        "duration": false}
    }
    

    【讨论】:

    • 好吧,这真的很简单——我花了两天时间才看到它!谢谢尼尔斯,感谢您的帮助:)
    • 没问题,我知道那种感觉;-)
    猜你喜欢
    • 2015-11-05
    • 2021-01-11
    • 2015-07-15
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多