在角度js中获得$ resource的错误配置

时间:2016-06-26 19:38:49

标签: javascript angularjs

我正在尝试使用rest service $ resource函数运行我的角度代码。我的代码中出现了错误的配置错误。我在我的基本网址中包含了#,所以我得到了,但如果我删除了它的显示404.请帮我正确地渲染我的页面。

这是app.js

'use strict';

angular.module('confusionApp', ['ui.router','ngResource'])
.config(function($stateProvider, $urlRouterProvider) {
        $stateProvider

            // route for the home page
            .state('app', {
                url:'/',
                views: {
                    'header': {
                        templateUrl : 'views/header.html',
                    },
                    'content': {
                        templateUrl : 'views/home.html',
                        controller  : 'IndexController'
                    },
                    'footer': {
                        templateUrl : 'views/footer.html',
                    }
                }

            })

            // route for the aboutus page
            .state('app.aboutus', {
                url:'aboutus',
                views: {
                    'content@': {
                        templateUrl : 'views/aboutus.html',
                        controller  : 'AboutController'                  
                    }
                }
            })

            // route for the contactus page
            .state('app.contactus', {
                url:'contactus',
                views: {
                    'content@': {
                        templateUrl : 'views/contactUs.html',
                        controller  : 'ContactController'                  
                    }
                }
            })

            // route for the menu page
            .state('app.menu', {
                url: 'dishes',
                views: {
                    'content@': {
                        templateUrl : 'views/menu.html',
                        controller  : 'MenuController'
                    }
                }
            })

            // route for the dishdetail page
            .state('app.dishdetails', {
                url: 'dishes/:id',
                views: {
                    'content@': {
                        templateUrl : 'views/dishdetail.html',
                        controller  : 'DishDetailController'
                   }
                }
            });

        $urlRouterProvider.otherwise('/');
    })
;

我的services.js

'use strict';

angular.module('confusionApp')
        .constant("baseURL","http://localhost:8080/app/index.html#/")
        .service('menuFactory',['$resource','baseURL', function($resource,baseURL) {

            var dishes=[
                         {
                          _id:0,
                          name:'Uthapizza',
                          image: 'images/uthapizza.png',
                          category: 'mains', 
                          label:'Hot',
                          price:'4.99',

                           comments: [
                               {
                                   rating:5,
                                   comment:"Imagine all the eatables, living in conFusion!",
                                   author:"John Lemon",
                                   date:"2012-10-16T17:57:28.556094Z"
                               },


                               {
                                   rating:4,
                                   comment:"Ultimate, Reaching for the stars!",
                                   author:"Ringo Starry",
                                   date:"2013-12-02T17:57:28.556094Z"
                               },
                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                        },
                        {
                          _id:1,
                          name:'Zucchipakoda', 
                          image: 'images/zucchipakoda.png',
                          category: 'appetizer', 
                          label:'',
                          price:'1.99',
                          description:'Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce',
                          comments: [


                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                        },
                        {
                          _id:2,
                          name:'Vadonut', 
                          image: 'images/vadonut.png',
                          category: 'appetizer', 
                          label:'New',
                          price:'1.99',
                          description:'A quintessential ConFusion experience, is it a vada or is it a donut?',
                           comments: [
                               {
                                   rating:5,
                                   comment:"Imagine all the eatables, living in conFusion!",
                                   author:"John Lemon",
                                   date:"2012-10-16T17:57:28.556094Z"
                               },


                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                        }
                        ];
            var promotions = [
                {
                          _id:0,
                          name:'Weekend Grand Buffet', 
                          image: 'images/buffet.png',
                          label:'New',
                          price:'19.99',

                }

            ];

                this.getDishes = function(){

                    return $resource(baseURL+"dishes/:id",null,{'update':{method:'PUT'}});

                };

              /*  this.getDish = function (index) {

                    return $http.get(baseURL,"dishes/"+index;
                };*/

                    this.getPromotion=function(index){
                        return promotions[index];
                    };




        }])

        .factory('corporateFactory', function() {

            var corpfac = {};

            var leadership = [
                {
                    name: "Peter Pan",
                    image: 'images/alberto.png',
                    designation: "Chief Epicurious Officer",
                    abbr: "CEO",

                },
                {
                    name: "Dhanasekaran Witherspoon",
                    image: 'images/alberto.png',
                    designation: "Chief Food Officer",
                    abbr: "CFO",

                },
                {
                    name: "Alberto Somayya",
                    image: 'images/alberto.png',
                    designation: "Executive Chef",
                    abbr: "EC",

                }

            ];

        corpfac.getLeaders=function(){
            return leadership;
        }
        corpfac.getLeader=function(index){
            return leadership[index];
        }

        return corpfac;




        })

;

这是我的controller.js

'use strict';

angular.module('confusionApp')

        .controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) {
            $scope.showMenu=true;
            $scope.message="loading...";
            $scope.tab = 1;
            $scope.filtText = '';
            $scope.showDetails = true;
            /* $scope.dishes=[];*/
            $scope.dishes= menuFactory.getDishes().query(
             function(response) {
                    $scope.dishes = response;
                    $scope.showMenu = true;
                },
                function(response) {
                    $scope.message = "Error: "+response.status + " " + response.statusText+JSON.stringify(response);
                });






                        }])



        .controller('DishDetailController', ['$scope', '$stateParams', 'menuFactory', function($scope, $stateParams, menuFactory) {

              $scope.dish=  menuFactory.getDishes.get({id:parseInt($stateParams.id,10)})


            .$promise.then(
            function(response){
                $scope.dish=response.data;
                $scope.showDish=true;
            },
                   function(response) {
                                $scope.message = "Error: "+response.status + " " + response.statusText;
                            }
            );

        }])

        .controller('DishCommentController', ['$scope','menuFactory', function($scope,menuFactory) {

            $scope.mycomment = {rating:5, comment:"", author:"", date:""};


                        $scope.submitComment = function () {
                                $scope.mycomment.date = new Date().toISOString();
                console.log($scope.mycomment);
                                $scope.dish.comments.push($scope.mycomment);

                menuFactory.getDishes().update({id:$scope.dish.id},$scope.dish);
                                $scope.commentForm.$setPristine();
                                $scope.mycomment = {rating:5, comment:"", author:"", date:""};
            }
        }])

        .controller('IndexController',['$scope','menuFactory','corporateFactory',function($scope,menuFactory,corporateFactory){
            $scope.promotion=menuFactory.getPromotion(0);
           $scope.leader= corporateFactory.getLeader(3);

$scope.showDish = true;
                        $scope.message="Loading ...";
                        $scope.feturedDish = menuFactory.getDishes().get({id:0});

 console.log($scope.feturedDish)

        }])

        .controller('AboutController',['$scope','corporateFactory',function($scope,corporateFactory){
            $scope.leaders=corporateFactory.getLeaders();

        }])




;

0 个答案:

没有答案