在Angular控制器中获取标头控制器未定义的错误

时间:2018-11-08 15:32:50

标签: angularjs

我收到了headercontroller未定义的错误。我已经在Index.html中引用了这个controller.js文件,还有什么我想念的

Controller.js

(function () {
    'use strict';
    angular
        .module('myApp')
        .controller('headerController', headerController);

    headerController.$inject = ['$scope', '$http' ,'$rootScope'];
    function headerController($scope, $http) {
        var vm = this;
    }

})();

app.js

(function () {

    var myapp = angular.module('myApp', ["ui.router"]);

    myapp.config(function ($stateProvider, $locationProvider, $urlRouterProvider) {

        // For any unmatched url, send to /route1
        $urlRouterProvider.otherwise("/route1")

        $stateProvider
          .state('route1', {
              url: "/route1",
              templateUrl: "SOC/Views/route1.html",
              controller: "route1ctrl"
          })
          .state('route2', {
              url: "/route2",
              templateUrl: "SOC/Views/route2.html",
              controller: "route2ctrl"
          })

        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
    });


})();

Index.html

  <script src="app.js"></script>
    <script src="SOC/Directives/Header/controller.js"></script>

我甚至在下面的代码中都尝试过它不起作用

angular
    .module('myApp', [])
    .controller('headerController', headerController);

1 个答案:

答案 0 :(得分:0)

您将通过提供空数组覆盖Angular模块的依赖项

angular
    .module('myApp', [])
    .controller('headerController', headerController);

从模块('myApp)中删除数组

angular
.module('myApp')
    .controller('headerController', headerController);

Controller.js

(function () {
    'use strict';
    function headerController($scope, $http) {
        var vm = this;
    }
    headerController.$inject = ['$scope', '$http' ,'$rootScope'];

    angular
        .module('myApp')
        .controller('headerController', headerController);
})();

有关在angularjs中$ inject的更多信息-https://docs.angularjs.org/guide/di