gpt4 book ai didi

javascript - AngularJS:为什么我的指令不能使用 $scope?

转载 作者:行者123 更新时间:2023-11-29 15:34:27 25 4
gpt4 key购买 nike

我写了一个非常简单的 Angular 模块,允许选项卡式导航(代码被简化,但也不起作用):

module.js

define(["angular","./controller","./tabs","./pane"],function(tabsController,tabs,pane){

var module = angular.module("tabsModule",[])
.controller("tabsController",["$scope",tabsController])
.directive("tabs",tabs)
//.directive("pane",pane);
return module;
});

tabs.js

define([], function() {
function tabs() {
var directive = {
restrict: "E",
controller: "tabsController",
scope: true,
templateUrl: "html/directives/tabs.html",
link: {
pre: function(scope, element, attrs, controller) {
scope.addPane = controller.addPane.bind(controller);
scope.select = controller.select.bind(controller);

}
},
// transclude: true,
};
return directive;
}
return tabs;
});

controller.js

define(["controllers/prototypes/base_controller"],function(BaseController){
var TabController=BaseController.extend({
constructor:function($scope){
BaseController.call(this,$scope);
this.$scope.panes = [];
this.directivesEvents=directivesEvents;
},
addPane:function(pane) {
if (pane.order === 0) {
this.$scope.select(pane);
}
this.$scope.panes = this.$scope.panes.concat(pane).sort(function(a, b) {
return a.order - b.order;
});
},
select:function(pane) {
angular.forEach(this.$scope.panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
this.$scope.$emit(this.directivesEvents.TAB_CHANGE, pane.id);
}
});
var TabController=function($scope){

};
TabController.$inject=["$scope"];
return TabController;
});

然后我将模块包含在另一个模块中:

var directives=angular.module("directives",["tabsModule"]);

但是当我使用它的时候,我得到了这个错误:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- tabsDirective

我不知道它是从哪里来的,我制作了几十个模块/指令,我想我已经像往常一样制作了这个......

我坚持了几个小时,请帮忙 !!!!

编辑:我没有指定它,但我使用的是 requirejs,这是导致此问题的原因。

最佳答案

错误是你没有将 tabs 函数传递给 angular 的指令方法。查看参数不匹配:

define(["angular","./controller","./tabs","./pane"],function(tabsController,tabs,pane){

改为:

define(["angular","./controller","./tabs","./pane"],function(angular, tabsController, tabs, pane){

关于javascript - AngularJS:为什么我的指令不能使用 $scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075558/

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