简体   繁体   English

使用Angular.js添加控制器时出错

[英]Getting error while adding controller using Angular.js

I am getting the following error while setting the controller through UI-ROUTER using Angular.js. 使用Angular.js通过UI-ROUTER设置控制器时出现以下错误。

Error: [ng:areq] http://errors.angularjs.org/1.4.6/ng/areq?p0=productDataController&p1=not%20aNaNunction%2C%20got%20undefined
    at Error (native)
    at http://oditek.in/medilink_global/js/angularjs.js:6:416
    at qb (http://oditek.in/medilink_global/js/angularjs.js:22:131)
    at Sa (http://oditek.in/medilink_global/js/angularjs.js:22:218)
    at http://oditek.in/medilink_global/js/angularjs.js:80:81
    at q (http://oditek.in/medilink_global/js/angularuirouter.js:7:14338)
    at http://oditek.in/medilink_global/js/angularuirouter.js:7:14796
    at $ (http://oditek.in/medilink_global/js/angularjs.js:73:89)
    at K (http://oditek.in/medilink_global/js/angularjs.js:62:39)
    at h (http://oditek.in/medilink_global/js/angularjs.js:54:410)

I am explaining my code below. 我在下面解释我的代码。

route.js: route.js:

var Admin=angular.module('medilink',['ui.router']);
Admin.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/product');
    $stateProvider
     .state('product', { 
            url: '/product',
            templateUrl: 'productview/product.html',
            controller: 'productController'
        })
    .state('product.catagory',{
        url:'/catagory',
        templateUrl:'productview/catagory.html',
        controller:'catagoryController'
    })
    .state('product.productinfo',{
        url:'/info',
        templateUrl:'productview/productinfo.html',
        controller:'productInfoController'
    })
    .state('product.productdata',{
        url:'/data',
        templateUrl:'productview/productdata.html',
        controller:'productDataController'
    })
});

productDataController.js: productDataController.js:

var info=angular.module('medilink');
    info.controller('productDataController',function($state,$http,$window,$scope){
        $scope.buttonName="Add";
    });
    info.directive('customOnChange', function() {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          var onChangeHandler = scope.$eval(attrs.customOnChange);
          element.bind('change', onChangeHandler);
        }
      };
    });

Here when i am including the controller name inside this controller page its throwing such type of error.Please help me to resolve this error. 在这里,当我在此控制器页面中包含控制器名称时,会引发此类错误。请帮助我解决此错误。

As the error says my guess is that you are not including productDataController.js file into your main HTML file. 正如错误所言,我的猜测是您没有在主HTML文件中包含productDataController.js文件。

<script src="/js/productDataController.js"></script>

Angular error page 角度错误页面

Well, when you calling angular.module() you create new module. 好吧,当您调用angular.module()您将创建一个新模块。 In official documentation said that this function is also for retrieving modules, but AFAIK, its better to register you controller in your main global module definition. 在官方文档中说该功能也用于检索模块,但是AFAIK最好在主全局模块定义中注册控制器。

In your case, angular.module() does not returning a module. 在您的情况下, angular.module()不返回模块。 I can assume that angular tries to create a new module. 我可以假设angular尝试创建一个新模块。

So your main module file should be: 因此,您的主模块文件应为:

var Admin=angular.module('medilink',['ui.router']);
Admin.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/product');
    $stateProvider
     .state('product', { 
            url: '/product',
            templateUrl: 'productview/product.html',
            controller: 'productController'
        })
    .state('product.catagory',{
        url:'/catagory',
        templateUrl:'productview/catagory.html',
        controller:'catagoryController'
    })
    .state('product.productinfo',{
        url:'/info',
        templateUrl:'productview/productinfo.html',
        controller:'productInfoController'
    })
    .state('product.productdata',{
        url:'/data',
        templateUrl:'productview/productdata.html',
        controller:'productDataController'
    })
});
Admin.controller('productDataController',function($state,$http,$window,$scope){
        $scope.buttonName="Add";
    });
    info.directive('customOnChange', function() {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          var onChangeHandler = scope.$eval(attrs.customOnChange);
          element.bind('change', onChangeHandler);
        }
      };
    });

Also its a good practise to split all your definitions into separate files and only use names of your constructor function 将所有定义拆分为单独的文件并且仅使用构造函数的名称也是一种好习惯

module.js: module.js:

var module = angular.module("module_name", [ "dependencies" ]);
module.controller("controllerNameInUse", controllerConstructor);
module.config(configConstructor)

controller.js: controller.js:

var controllerConstructor = function($scope) {
    //your controller code here
};
controllerConstructor.$inject = ["$scope"]

config.js: config.js:

var configConstructor = function($scope) {
    // your config code here
};
configConstructor.$inject = ["$scope"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM