首页 > angularjs model 的控制器没有调用

angularjs model 的控制器没有调用

代码如下:

angular.module('picker', {
}).directive('datepicker', function(){
    return {
        controllerAs:"datepickCtr",
        restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
        templateUrl: 'datepicker.html',
        replace:true,
        //templateUrl: '',
        // compile: function(tElement, tAttrs, function transclude(function(scope, cloneLinkingFn){ return function linking(scope, elm, attrs){}})),
        link: function($scope, element, iAttrs) {
            console.log("datepicker.html");//这个被打印
        }
    };
}).controller('datepickCtr', function(){
    console.log("asd");//这个没有被打印
}).run(function ($templateCache){
      $templateCache.put('datepicker.html',
        '<div>asda</div>'
      );
})

如代码所示,LINK部分正常打印,但是控制器没有被打印,求解

controllerAs那里错了。

如果你喜欢controllerAS语法的话,

javascriptcontroller: "DatepickCtrl as datepickCtrl" //话说你的DatepickCtrl开头可以大写吗!!

或者

javascriptcontroller: "DatePickCtrl",
controllerAs: "datepickCtrl"

controllerAs -> controller

【热门文章】
【热门文章】