如何将此指令注入AngularJs控制器?

时间:2017-01-22 12:21:56

标签: angularjs dependency-injection

我想使用代码格式this Plunk

它有一个指令:

myApp.directive('fileModel', ['$parse', function ($parse) {

和服务

myApp.service('fileUpload', ['$http', function ($http) {

并且示例因此注入控制器:

myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){

我有这个:

angular
    .module('Dashboard')
    .controller('DashboardController', DashboardController);

function DashboardController($rootScope, $scope, $http, 
                             $interval, $state, $location)
{

但我无法弄清楚如何将文件上传注入我的控制器: - (

1 个答案:

答案 0 :(得分:1)

试试这样。我认为您的问题是模块名称。在fileUpload的同一模块中定义DashboardController服务

  var myApp = angular.module("yourAppName",[]);
  myApp.directive('fileModel', ['$parse', function ($parse) {
  ...

  myApp.service('fileUpload', ['$http', function ($http) {
  ...

 myApp.controller('DashboardController', DashboardController);


function DashboardController($rootScope, $scope, $http, 
                         $interval, $state, $location,fileUpload)
   {