I'm using angularjs and I wrote a combobox and a tabs directive for my application which I want to use like this:
<tabs>
<tab>Some content</tab>
<tab>Some other content
<my-combobox options="options" selection="selection">
</tab>
</tab>
My problem is that at the moment, I can't watch properties changed by the combobox when it's inside a tab, meaning if I do this:
$scope.$watch("selection", function(){
console.log("this code doesn't run when selection changes")
});
My watch doesn't fire when the user selects item from the combobox.
What puzzles me is that it does work when I take the combobox out of the tab
<!-- The watch works where -->
<my-combobox options="options" selection="selection">
<tabs>
<tab>Some content</tab>
<tab>Some other content</tab>
</tab>
So my question is how are the tabs affecting the watch of elements transcluded in them and how to fix it?
this is the code for the tabs directive:
angular.module("ip").directive("tabs", [function () {
return {
restrict: "E",
replace: true,
transclude: true,
templateUrl: "/Content/Directives/tabs.html",
bindToController: true,
controllerAs: "tabsController",
scope: {
onTabSelected: "=?"
},
controller: function () {
var self = this;
self.tabs = [];
self.addTab = function (tab) {
self.tabs.push(tab);
if (self.tabs.length === 1) {
tab.active = true;
self.onTabSelected(tab);
}
}
self.select = function (selectedTab) {
angular.forEach(self.tabs, function (tab) {
if (tab.active && tab !== selectedTab) {
tab.active = false;
}
});
selectedTab.active = true;
if (self.onTabSelected) { self.onTabSelected(selectedTab); }
}
}
};
}]);
(Edit) This is my code for the tabs.html file:
<div class="ip-tabs">
<ul>
<li role="presentation" ng-repeat="tab in tabs" ng-click="select(tab)" ng-class="tab.active ? 'active' : ''">
<span>{{tab.tabTitle}}</span>
</li>
</ul>
<ng-transclude>
</ng-transclude>
</div>
And the tab.js directive
angular.module("ip").directive("tab", [function () {
return {
restrict: "E",
replace: true,
transclude: true,
templateUrl: "/Content/Directives/tab.html",
require: "^tabs",
scope: {
tabTitle: "@"
},
link: function (scope, element, attr, tabsCtrl) {
scope.active = false;
tabsCtrl.addTab(scope);
}
};
}]);