为什么此控制器内的console.log和函数不起作用?

时间:2015-03-23 22:20:21

标签: angularjs angularjs-directive angularjs-scope

console.log语句没有记录任何内容,我的控制器函数中的其余代码也没有:

var app = angular.module('myApp.manage.alertDirectives', [])
.directive('alert-bar', function () {

    return {
        template: 
            '<section ng-show="showAlert" ' +
            'ng-click="closeNotification()" ' +
            'class="ng-notification"> ' +
            '<p class="alert-msg">{{alert_message}}</p> ' +
            '<div class="alert-bg {{alert_type}}"></div> ' +
            '</section>',
        restrict: 'E',
        controller: function($scope,
                             ScopeFactory) {

            console.log('inside controller for alerts Directive:');
            var vs = $scope;
            ScopeFactory.saveScope('alerts', vs);
        }
    };
});

然而,在我的另一个指令控制器中,那里的代码正常工作!

var app = angular.module('myApp.manage.termsDirectives', 
['myApp.manage.alertDirectives'])

.directive('termsForm', function() {
    return {
        templateUrl: "manage/terms/termsForm.html",
        restrict: "E",
        controller: function($scope,
                             $location,
                             ApiFactory,
                             TermsFactory,
                             TermsStringFactory,
                             ScopeFactory) {

            console.log('inside controller for termsForm Directive:');

<alert-bar></alert-bar>位于我的页面顶部,<terms-form></terms-form>位于另一个控制器内:

<div class="container" ng-view></div>包含以下内容:

<div class="col-sm-12 terms-container" ng-controller="TermsCtrl as tc">
    <div class="row" id="term-form-block">
        <terms-form></terms-form>

1 个答案:

答案 0 :(得分:3)

<alert-bar>的指令规范化名称应该是驼峰式的:

.directive("alertBar", function(){...})

注册指令时 - 就像"termsForm"一样。