我正在使用带有路线的 Angular 服务,其中我的服务文件是单独的。 我在控制台中遇到了branchService is undefined 错误。请参阅 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);
}
当我运行错误:$injector:modulerr 模块错误错误显示在控制台中
最佳答案
您是否在index.html 或您的首页中添加了对branchService.js 文件的引用。
尝试在 myController.js 之后添加引用。
关于javascript - 使用路由的未定义服务 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836525/