Struggling to find out the best way to watch for attribute changes, which would ideally update based on a keypress event, bound to the scope in the parent controller
I would like each 'instance' of the directive to have its own 'hasFocus' property to be changed by updating the attribute value e.g.
<menu has-focus="{{ true }}" ></menu>
<menu has-focus="{{ false }}" ></menu>
<menu has-focus="{{ false }}" ></menu>
template:
<div class="menu">
<ul>
<li ng-repeat="option in menu.options" ng-class="{ 'focused' : hasFocus }" tabindex="{{ $index }}">
<div class="icon">{{ $index+1 }}</div>
</li>
</ul>
and so only 1 directive can have the value equal to 'true' at any one time.
I have a custom directive
.directive('menu', function()
{
restrict : 'E',
templateUrl : 'partials/menu-left.html',
replace : true,
scope : {
hasFocus : '@'
},
link : function( $scope, element, attrs )
{
attrs.$observe('hasFocus', function(value) {
console.log(value);
});
}
})
but cant seem to extract the value from the $observe method tried using $watch but still didnt work not sure what I am doing wrong!
hasFocus:@which takes the true/false values as strings. You need to get them as objects usinghasFocus:"="andhas-focus="true"on the HTML