0

I have defined a scope in my directive.

scope.selection = 3

When I apply the directive to an element,

<div myDirective></div>

its template would print the scope var correctly.

<span>{{selection}}</span> //prints: 3

but when I update the scope variable in the directive it does not update in the view.

element.bind("keydown", function(event){
    if(event.which === 38){ //up arrow
        scope.selection--;
        console.log(scope.selection); //logs the updated value but view stays the same
    }
}

Can I bind scopes from a directive?

EDIT:

If I view a different browser tab then go back to the app the view is then updated.

wazzaday
  • 9,474
  • 6
  • 39
  • 66
  • 1
    you need to call `scope.$apply()`, see http://jimhoskins.com/2012/12/17/angularjs-and-apply.html – hgoebl Dec 20 '14 at 16:23
  • You need to do `$scope.$digest()`; `$scope.$apply()` would also work, but it unnecessarily starts a digest from `$rootScope` – New Dev Dec 20 '14 at 16:23
  • Ok thanks guys both of those work, I thought as a keypress event was firing this would not be needed. – wazzaday Dec 20 '14 at 16:24
  • possible duplicate of [Angular.js How update scope in directive?](http://stackoverflow.com/questions/19154539/angular-js-how-update-scope-in-directive) – New Dev Dec 20 '14 at 16:32

0 Answers0