gpt4 book ai didi

javascript - AngularJS:分割到新文件时未调用工作指令

转载 作者:行者123 更新时间:2023-11-28 06:56:09 25 4
gpt4 key购买 nike

问题:为什么在将模块拆分为单独的文件后,我的(之前有效的)AngularJS 指令没有被调用?

背景:我的明信片模块包含在一个文件中。我试图将其分开,但没有成功。 postcards.factoriespostcards.controllers 文件工作正常。我无法让 postcards.directives 中的单个指令发挥作用。我已经成功地用一个不同的、更复杂的模块做到了这一点。

研究:我读过几篇 SO 帖子,例如 this onethis one没有太多运气。这些帖子似乎重点关注 module 的初始声明,而没有所需的 []

主模块 - 明信片

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

明信片.指令

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

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

明信片 Controller

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) {...}

明信片工厂

// 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
}

主应用程序变量 - 显示明信片作为依赖项包含在内

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', [...]) 中时,我并不太理解。声明。

最佳答案

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

<num-messages></num-messages>未被同事重构以反射(reflect)指令名称更改为 numPostcards 。我没有检查过。

现在已成功调用 <num-postcards></num-postcards> .

抱歉,@Phil。

关于javascript - AngularJS:分割到新文件时未调用工作指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com