javascript - 使用路由的未定义服务 Controller

标签 javascript angularjs ngroute

我正在使用带有路线的 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/

相关文章:

javascript - ExtJS网格过滤器: how can I load 'list' filter options from external json?

javascript - javascript 中的 css 重要不工作

javascript - 如何结合 ng-if 、 ng-click 和 ng-show 并一起使用它们

javascript - 如何在 Angular js 中处理 ngRoute View 加载上的多个资源调用

javascript - Angular ngroute : controllers doesnt's work

javascript - ReactJS:状态更改时元素不会更新

javascript - 具有计算列的 Angularjs x 可编辑表

javascript - 在生产环境中,Webpack 2.2.0.rc-0 将丑化/缩小vendor.js和manifest.js,但不会丑化/缩小bundle.js

javascript - Angular : how to set 'value' different than displayed value in options?

javascript - AngularJs 路由没有错误但不工作