AngularJS: Why can't my directive use $ scope?
I wrote a really simple angular module that allows tabbing (code simplified but doesn't work):
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;
});
and I include the module in another:
var directives=angular.module("directives",["tabsModule"]);
But when i use it i got this error:
Error: [$ injector: unpr] Unknown provider: $ scopeProvider <- $ scope <- tabsDirective
I have no idea where it came from, I made some modules / directives and I think I did it as always.
I've been stuck on it for hours, help !!!!
Edit: I didn't specify it, but I used requirejs and that caused this problem.
source to share
The error is that you are not passing the tabs function to the angular directive method. See Parameter Mismatch:
define(["angular","./controller","./tabs","./pane"],function(tabsController,tabs,pane){
Instead, do:
define(["angular","./controller","./tabs","./pane"],function(angular, tabsController, tabs, pane){
source to share
Finally, I just got it, I forgot to add it angular
as a parameter in module.js
, so every argument was in the wrong place.
define(["angular","./controller","./tabs","./pane"],function(angular,tabsController,tabs,pane){
var module = angular.module("tabsModule",[])
.controller("tabsController",["$scope",tabsController])
.directive("tabs",tabs)
//.directive("pane",pane);
return module;
});
source to share