Angular.js解决抛出未知提供程序错误

时间:2014-05-20 14:52:09

标签: ruby-on-rails angularjs

我已经查看了有关此主题的一些相关问题和答案,但似乎没有一个适合我的情况,因为我无法在没有错误的情况下解决问题。

我尝试按照教程了解有关Angular路线的解决方法,在我现有的应用中尝试此示例:http://www.undefinednull.com/2014/02/17/resolve-in-angularjs-routes-explained-as-story/

我收到此错误,似乎可能与我正在使用的解析和ng-view有关?任何帮助将不胜感激。对Angular而言还是新手。在后端使用Rails:

  

错误:[$ injector:unpr]未知提供商:myFriendsHotelProvider< -   myFriendsHotel   http://errors.angularjs.org/1.2.6/ $注射器/ unpr?P0 = myFriendsHotelProvider%20%3 C-%20myFriendsHotel       在http://lala.dev/assets/angular.js?body=1:79:12       在http://lala.dev/assets/angular.js?body=1:3539:19       at Object.getService [as get](http://lala.dev/assets/angular.js?body=1:3666:39)       在http://lala.dev/assets/angular.js?body=1:3544:45       at getService(http://lala.dev/assets/angular.js?body=1:3666:39)       在调用(http://lala.dev/assets/angular.js?body=1:3688:13)       在Object.instantiate(http://lala.dev/assets/angular.js?body=1:3709:23)       在http://lala.dev/assets/angular.js?body=1:6759:28       在链接(http://lala.dev/assets/angular-route.js?body=1:907:26)       在nodeLinkFn(http://lala.dev/assets/angular.js?body=1:6213:13

app.js.coffee

window.lala = angular.module "Lala", ["angularFileUpload", "ngResource", "ngAnimate", "ngRoute", "ui.bootstrap", "ui.bootstrap.setNgAnimate", "ui.sortable", 'ngTagsInput']

lala.config ['$httpProvider', ($httpProvider) ->
  authToken = $("meta[name=\"csrf-token\"]").attr("content")
  $httpProvider.defaults.headers.common["X-CSRF-TOKEN"] = authToken
]

lala.config ['$routeProvider', ($routeProvider) ->
  $routeProvider
    .when("/accomodation",
      templateUrl: "/assets/templates/list.html"
      controller: "AccomodationCtrl"
      resolve: ->
        myFriendsHotel: ->
          hotelName: ->
            "My Friend's Hotel"
          roomNumber: ->
            "100"
    )
    .when("/home",
      templateUrl: "/assets/templates/home.html"
      controller: "HomePageCtrl"
    )
    .when("/tags/:tag",
      templateUrl: "/assets/templates/lists.html"
      controller: "ListsCtrl"
    )
    .when("/:filter",
      templateUrl: "/assets/templates/lists.html"
      controller: "ListsCtrl"
    )
    .when("/users/:id",
      templateUrl: "/assets/templates/profile.html"
      controller: "UsersCtrl"
    )

    .otherwise(redirectTo: '/recent')
]

accomodation_controller.js.coffee

lala.controller "AccomodationCtrl", ["$scope", "myFriendsHotel", ($scope, myFriendsHotel) ->
  $scope.name = "Shidhin"
  $scope.place = "Switzerland"
  $scope.hotel = myFriendsHotel.hotelName()
  $scope.roomno = myFriendsHotel.roomNumber()
]

list.html.haml

.row
  .col-xs-12.col-sm-6.col-md-8
    %div
      %h2 Hi, {{name}}. Welcome to {{place}}
      %div The accomodation is arranged on <strong>{{hotel}}</strong> and room number is <strong>{{roomno}}</strong>
  .col-xs-6.col-md-4
    #right-column
      great work

angular.html.haml

!!!
%html
  %head
    %title Lala
    %meta{content: "width=device-width, initial-scale=1.0", name: "viewport"}/
    = favicon_link_tag "lala_favicon.ico"
    = stylesheet_link_tag "http://fonts.googleapis.com/css?family=Lato"
    = stylesheet_link_tag "application", :media => "all"
    = javascript_include_tag "application"
    = csrf_meta_tags

    :javascript
      window.current_user_name = "#{current_user.try(:username)}" 
      window.current_user_image = "#{avatar_url(current_user) unless !current_user}"


  %body{"ng-app" => "Lala"}
    = render :partial => "shared/navbar"
    #page-content{iScroll: ""}
      .container.bordered
        .row
          .col-xs-12.col-sm-12.col-md-12.col-lg-12
            .breadcrumb
              {{breadcrumb}}
        %div{"ng-view" => true}
          = yield
    #scroll-top
      %a{:href => "#"}
        back to top
        %span.glyphicon.glyphicon-arrow-up

1 个答案:

答案 0 :(得分:-1)

任何时候出现Unknown provider错误,都是因为Angular不知道如何提供您要求的依赖关系。该错误告诉您哪个依赖项是:

Unknown provider: myFriendsHotelProvider <- myFriendsHotel

那么,问题是,为什么Angular不能注入myFriendsHotel?检查确定的位置,并确保其中包含的任何模块都包含在您的应用中。