创建服务时未知的提供者 - Angular

时间:2016-04-03 15:43:15

标签: angularjs ionic-framework

我正在努力学习离子和放大器通过制作基本的短信应用来实现角度。所以我开始使用离子提供的标签模板,我只是调整一下来弄清楚一切是如何运作的。

无论如何,我试图将会话ID从表格行传递到会话控制器,以便它可以使用相关信息填充视图。

这是我的控制器:

var baxter = angular.module('baxter');

baxter.controller('ConversationsCtrl', function($scope, 

conversationService) {
  $scope.conversations = conversationService.conversations;

  $scope.setCurrentConversation = function(convoID){
    conversationService.selectedConversation(convoID);
  }
});

服务:

var baxter = angular.module('baxter');

baxter.service('conversationService', function($http) {
  this.conversations = [
    { title: 'Jill Sanders', id: 1 },
    { title: 'Bobby Duck', id: 2 },
    { title: 'Boatie McBoatface', id: 3 },
    { title: 'Jesus Christ', id: 4 },
    { title: 'Tall Blonde', id: 5 },
    { title: 'Mom', id: 6 }
  ];

  this.selectedConversation = function(conversationID) {
    console.log(conversationID);
  }

  return this;
});

错误:

0     780569   error    Error: [$injector:unpr] Unknown provider: conversationServiceProvider <- conversationService <- ConversationsCtrl

1 个答案:

答案 0 :(得分:-1)

当您再次重新声明baxter时,您将覆盖变量var baxter。这将创建一个新实例,之前的声明将丢失

为了不担心加载顺序而不使用全局变量来表示模块,我建议你不要使用var来引用模块,只需这样做:

angular.module('baxter').controller('ConversationsCtrl', ... ;

angular.module('baxter').service('conversationService'....

如果一个文件中有多个组件,您也可以链接它们:

angular.module('baxter')
     .controller('ConversationsCtrl',function(/* dependencies*/){

     })
     .service('conversationService'....

参考:John Papa Angular 1 Style Guide

相关问题