AngularJS:拆分为新文件时未调用Working指令

时间:2015-09-14 01:32:00

标签: javascript angularjs

问题:为什么在将模块拆分成单独的文件后调用我的(以前工作的)AngularJS指令?

背景:我的postcards模块包含在一个文件中。我正在尝试分裂它,但没有成功。 postcards.factoriespostcards.controllers文件正常运行。我无法在postcards.directives中获得单个指令来运行。我用一个不同的,更复杂的模块成功地做到了这一点。

研究:我已阅读了几篇SO帖子,例如this onethis one没有太多运气。这些帖子似乎侧重于module的初始声明,没有必要的[]

主要模块 - 明信片

var postcardsApp = angular.module('postcards', ['postcards.directives', 'postcards.factories', 'postcards.controllers']);

postcards.directives

var postcardAppDirectives = angular.module('postcards.directives', ['postcards.controllers']);

postcardAppDirectives.directive('numPostcards', function () {
    return {
        restrict: "AE",
        template: '{{ ctrl.numPostcards }}',
        controller: 'postcardDirectiveController as ctrl'
    }
});

postcards.controllers

var PostcardsControllers = angular.module('postcards.controllers', ['postcards.factories']);

PostcardsControllers.controller("postcardDirectiveController", ['PostcardFactory',
    function (PostcardFactory) {
        var _this = this;
        PostcardFactory.getNumPostcards().$promise.then(function (data) {
            console.log(data);
            _this.numPostcards = data['numPostcards'];
        });
    }]);

// This controller works fine.
PostcardsControllers.controller('PostcardMainController', ['PostcardFactory', function (PostcardFactory) {...}

postcards.factories

// These are retrieving data fine.
var POSTCARD_URL = 'http://' + BASEURL + '/api/v1/postcards/';

var PostcardsFactories = angular.module('postcards.factories', []);

PostcardsFactories.factory('Inbox', ['$resource', function ($resource) {
    return $resource(POSTCARD_URL);
}
]);

// More factories pulling data from my API here...

PostcardsFactories.factory('PostcardFactory', ['$http', 'Inbox', 'Sent', 'PostcardDetail', 'NewPostcard', 'UnreadCount', '$q',
    function ($http, Inbox, Sent, PostcardDetail, NewPostcard, UnreadCount, $q) {

        var PostcardFactory = {}

        // Get count of all unread messages sent TO a member
        PostcardFactory.getNumPostcards = function () {
            return UnreadCount.query()
        };

        ...  // Tons of stuff in here

        return PostcardFactory
    }

Main App var - 显示postcards作为依赖项包含

var app = angular.module('mainApp', ['ngResource', 'boat', 'members', 'location', 'trips',
    'angular-jwt', 'tools', 'carousel', 'navbar', 'dashboard', 'postcards', 'widgets', 'ui.bootstrap', 'ui.router']);

HTML调用指令

<li ng-if="navCtrl.isLoggedIn()">
    <a ui-sref="dashboard.postcards">
    <span class="glyphicon glyphicon-envelope" style="color: dodgerblue"></span>
    <span style="margin-left: -3px; margin-top: -15px; background-color: red; opacity: .75;" class="badge">
    <num-messages></num-messages>
    </span>
    </a>
</li>

包含在HTML文件中

<script src="./static/app/postcards/postcards.js"></script>
<script src="./static/app/postcards/postcards_factories.js"></script>
<script src="./static/app/postcards/postcards_controllers.js"></script>
<script src="./static/app/postcards/postcards_directives.js"></script>

问题重述:为什么我的指令不被调用?

其他问题:当我需要在.module('myModule', [...])声明中包含其他模块时,我真的不知道。

1 个答案:

答案 0 :(得分:0)

回答问题 - 尽可能以最愚蠢的方式。

<num-messages></num-messages>未被同事重构,以反映该指令的名称更改为numPostcards。我没有检查它。

现在使用<num-postcards></num-postcards>成功调用。

对不起,@ Phil。