2016-03-07 90 views
0

我正在使用角度服務的路由,我的服務文件是分開的。 和我面對branchService是console.See代碼未定義的錯誤Plunker code 這裏是branchService.js:未定義的服務控制器使用路由

angular.module("myApp").service('branchService', ['$http', function ($http) { 
    var link = "http://localhost:8008/"; 
    //----Get Dining Tables`enter code here` 
    this.getBranches = function ($scope) { 
     return $http({ 
      method: "GET", 
      url: encodeURI(link + "Branch/GetBranches/"), 
      headers: { 'Content-Type': 'application/json' } 
     }).success(function (data) { 
      console.log(data); 
     }).error(function (data) { 
      console.log(data); 
     }); 
    }; 
}]); 

和myController.js是在這裏:

var app = angular.module("myApp", ['ngRoute']); 

app.config(function ($routeProvider) { 
    $routeProvider 
     .when('/branches', { 
      templateUrl: 'branches.html', 
      controller: 'branchController' 
     }) 

     .otherwise({ 
      redirectTo: '/branches' 
     }); 
}); 

app.controller('branchController', ['$scope', '$filter', 'branchService', function ($scope, $filter, branchService) { 
    branchService.getBranches($scope); 
} 

當我運行錯誤:$注射器:modulerr 模塊錯誤錯誤顯示在控制檯中

+0

檢查Nilay科塔裏的答案。這可能只是你沒有將服務文件添加到你的index.html文件中,或者你的命名關閉了。 – groooves

+0

檢查'錯誤:$ injector:modulerr Module Error'之前的語法錯誤消息。應用控制器需要'}])'結束。 – georgeawg

+0

我已經在html文件中添加了所有js文件引用,並且控制檯未顯示任何語法錯誤 –

回答

0

在使用服務前先注入,所以你可以用它

var app = angular.module("myApp", ['ngRoute', 'branchService']); 

編輯:

這是branchController.js問題:你在再次注入ngRoute

var app = angular.module("myApp", ['ngRoute']); 

事實上,您已經將其添加到app.js中:

var app = angular.module("myApp",['ngRoute']);

要解決:刪除納克航線

var app = angular.module("myApp"); 
+0

我試過這個。 –

+0

@AdnanRRO仍然無法使用? – Sherlock

+0

是的,它不工作 –

1

您是否在index.html或任何第一頁中添加了對branchService.js文件的參照。

只需在您的myController.js之後添加參考。

+0

是的,我已經在html頁面中添加了所有引用js文件 –

+0

您的退信文件存在問題。嘗試下載並保證最新的角度和角度路由js文件。 –

0

你必須這樣做。你的服務應該在你的控制器之前先加載。

var app = angular.module("myApp", ['ngRoute']); 
 

 
app.config(function ($routeProvider) { 
 
    $routeProvider 
 
     .when('/branches', { 
 
      templateUrl: 'branches.html', 
 
      controller: 'branchController' 
 
     }) 
 

 
     .otherwise({ 
 
      redirectTo: '/branches' 
 
     }); 
 
}); 
 

 
app.service('branchService', ['$http', function ($http) { 
 
    var link = "http://localhost:8008/"; 
 
    //----Get Dining Tables`enter code here` 
 
    this.getBranches = function ($scope) { 
 
     return $http({ 
 
      method: "GET", 
 
      url: encodeURI(link + "Branch/GetBranches/"), 
 
      headers: { 'Content-Type': 'application/json' } 
 
     }).success(function (data) { 
 
      console.log(data); 
 
     }).error(function (data) { 
 
      console.log(data); 
 
     }); 
 
    }; 
 
}]); 
 

 
app.controller('branchController', ['$scope', '$filter', 'branchService', function ($scope, $filter, branchService) { 
 
    branchService.getBranches($scope); 
 
}

+0

我的服務文件是分開的。 –