I have a angular service that manages all my UI tabs. It contains an object that has all the information I need about a tab such as icons, labels, URL, etc. I have a use case for one my tabs to have a dynamic counter on it but I can't figure out how to get it working.
Service snippet:
angular.module('app.services').service('tabService', function($rootScope, $route, $location) {
var tabs = {
"/front": {
"search": {
label: "Search",
url: "/search",
classIcon: "fa fa-search",
urlMatchAddt: ['/searchResults','/productDetails']
},
"order": {
label: "Order",
url: "/cart",
classIcon: "fa fa-shopping-bag"
} ....
HTML Code: index.html (tabService is injected into a my BaseCtrl controller)
<body ng-app="app" ng-controller="BaseCtrl">
...
<li ng-repeat="t in tabService.getTabs()" ng-class="{active: tabService.isActiveTab(t.url)}" ng-cloak>
<a href="#{{t.url}}"><i class="{{t.classIcon}}" aria-hidden="true" ng-bind-html=""></i> <span ng-bind-html="t.label | trustHTML"></span></a>
</li>
....
</body>
So what I am trying to do for example is in the label field of one of my tabs I want to put something like
label: "Order - {{counter}}"
So every time I update that {{counter}} variable then my label will also refresh. The label can also contain HTML code which is why I am using ng-bind-html directive.
Currently I am doing an ugly $watch on the variable and when it changes then I am manually just completely overwriting the label value with a new string that includes the updated value.
I have tried using $compile but I cannot use it with $rootScope and I cannot pass $scope into my service. I am unsure what the best solution is.
Any ideas?
AngularJS: 1.6
<span>{{ t.label }} - {{$index}}</span>? Why are these creepy ng-bind-html there?labelattributes have html. Example would be mycounterwill be wrapped in a span tag to utilize a fontawesome icons