angular.js - angularjs 读取不出json文件
漂亮男人
漂亮男人 2017-05-15 17:11:09
[AngularJS讨论组]

参照Phonecat做的菜谱网站,在做路由的时候出了些问题,json数据不能显示在模板上。

//设置路由
angular.
    module('recipeApp').
    config(['$locationProvider', '$routeProvider', 
        function config($locationProvider, $routeProvider) {
            $locationProvider.hashPrefix('!');

            $routeProvider.
                when('/data', {
                    template: ''
                }).
                when('/data/:recipeId', {
                    template: ''
                }).
                otherwise('/data');
        }
    ]);

这个调用recipe-list是没问题的

//这个是模仿原版做的对HTTP封装
angular.
    module('recipequery').
    factory('Recipe', ['$resource', 
        function($resource) {
            return $resource('data/:recipeId.json', {}, {
                query: {
                    method: 'GET',
                    params: {recipeId: 'recipes'},
                    isArray: true
                }
            });
        }
    ]);

在recipe-list里调用的.query没有出问题

//这是注册recipedetail组件
angular.
    module('recipeDetail').
    component('recipeDetail',{
        templateUrl: 'recipe-detail/recipe-detail-template.html',
        controller: ['$routeParams', 'Recipe',
            function RecipeDetailController($routeParams, Recipe) {
                this.recipe = Recipe.get({recipeId: $routeParams.recipeId});
        }
      ]
    });

这里我和原版有些不一样,放弃了他的切换图片功能,这里我就瞎改了下,结果就是模板dom里的字都能看到,需要调取的json数据全没有,附上我的模板:

//recipe-detail-template.html

{{$ctrl.recipe.name}}

"

{{$ctrl.recipe.comment}}

"

Ingredients

  • {{x}}{{y}}

Steps

    {{step}}

漂亮男人
漂亮男人

全部回复(1)
某草草

自问自答小王子. 是json格式出错,突然对这个自己提的问题感到很羞耻

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号