How do I update an angularjs page after a scope update?

Antoine

I have written a directive that captures keyboard events, and on certain keys I update some objects in the scope. The idea is to move up and down an array and display the selected row details. The problem is the page is not updated until I do another action that updates the page. How can I force this?

Here's the directive:

LogApp.directive("keyCapture", [function(){
    var scopeInit;
    return{
        link: function(scope, element, attrs, controller){
            scopeInit = scope
            element.on('keydown', function(e){
                scopeInit[attrs.keyCapture].apply(null, [e]);                
            });
        }
    }
}]);

Bound the template like this:

<body ng-controller="logCtrl" key-capture="movePreview">

The controller method:

$scope.movePreview = function(e){
    if ($scope.events.length === 0)
        return;
    // Find the element 
    if (e.keyCode === 38 || e.keyCode === 40){
        console.log("Pressed %s", e.keyCode);
        var offset = 0;
        if (e.keyCode === 38 && $scope.previewIndex > 0)
            offset = -1;
        else if (e.keyCode === 40 && $scope.previewIndex < $scope.events.length -1 )
            offset = 1;

        $scope.previewIndex += offset;
        var eventId = $scope.events[$scope.previewIndex].uuid;
        $scope.showEvent(eventId);
        e.preventDefault();
    }
};

$scope.showEvent(eventId) will take the item with given ID and display it in another part of the page. Part that is not update until another action is performed, like clicking a button. Is it possible to force the page update?

Here's a fiddle that reproduces: http://jsfiddle.net/gM2KF/1/ If you click on the button, the counter is updated. If you press any key, nothing shows. But you click again on the button, you see the counter has been updated by the keyboard event, but didn't show.

Davin Tryon

If you are listening to non-angular events:

element.on('keydown', function(e){
    scopeInit[attrs.keyCapture].apply(null, [e]);                
});

Then, to update the scope, you need to call scope.$apply:

element.on('keydown', function(e){
    scopeInit[attrs.keyCapture].apply(null, [e]); 
    scope.$apply();               
});

This will kick off an angular $digest cycle and allow changes to bind.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Update parent scope variable in AngularJS

From Dev

How do I update normals after positioning vertices in vertex shader?

From Dev

AngularJs $scope doesn't update after a GET request on a factory

From Dev

How to update scope model after operating the filters?

From Dev

How to update an AngularJS array after sorting?

From Dev

How to update $scope outside the modal bootstrap in AngularJS?

From Dev

Angularjs how to update view after a REST operation?

From Dev

AngularJS how to dynamically update part of scope with variable

From Dev

How can I update the $scope in AngularJS when a server sided update occurs?

From Dev

Android Eclipse not working after Java Update how do I fix it?

From Dev

AngularJS: How can I update the $scope property

From Dev

AngularJS animate div on scope update

From Dev

AngularJS and RouteProvider how to update a small section in the page

From Dev

Atlassian Confluence : how do I update page using REST API

From Dev

How do I convert row into CLOB in the applied trigger after update?

From Dev

How to softly update $scope and template using Angularjs?

From Dev

AngularJS : directive does not update scope after $http response in parent scope

From Dev

How do I kill the lockscreen after the Windows 10 anniversary update?

From Dev

How do I enable Cortana after the Anniversary Update (1607)?

From Dev

How do I recover ubuntu 12.04 after failed hardware update?

From Dev

AngularJS/jQuery - Update Scope

From Dev

Update binding after server response / How do I selectively update bindings?

From Dev

How to update $scope variable value in AngularJs

From Dev

How do I get the html on the scope within a unit test to update after a button click event changes the active html?

From Dev

How do I make AngularJS update input values?

From Dev

How do I update an MKPointAnnotation image after downloading the right image?

From Dev

How do I programmatically calling Update IN after a shipment is confirmed?

From Dev

AngularJS directive scope variable doesnt update after reopening the window

From Dev

How do I fix broken boot after shim update?

Related Related

  1. 1

    Update parent scope variable in AngularJS

  2. 2

    How do I update normals after positioning vertices in vertex shader?

  3. 3

    AngularJs $scope doesn't update after a GET request on a factory

  4. 4

    How to update scope model after operating the filters?

  5. 5

    How to update an AngularJS array after sorting?

  6. 6

    How to update $scope outside the modal bootstrap in AngularJS?

  7. 7

    Angularjs how to update view after a REST operation?

  8. 8

    AngularJS how to dynamically update part of scope with variable

  9. 9

    How can I update the $scope in AngularJS when a server sided update occurs?

  10. 10

    Android Eclipse not working after Java Update how do I fix it?

  11. 11

    AngularJS: How can I update the $scope property

  12. 12

    AngularJS animate div on scope update

  13. 13

    AngularJS and RouteProvider how to update a small section in the page

  14. 14

    Atlassian Confluence : how do I update page using REST API

  15. 15

    How do I convert row into CLOB in the applied trigger after update?

  16. 16

    How to softly update $scope and template using Angularjs?

  17. 17

    AngularJS : directive does not update scope after $http response in parent scope

  18. 18

    How do I kill the lockscreen after the Windows 10 anniversary update?

  19. 19

    How do I enable Cortana after the Anniversary Update (1607)?

  20. 20

    How do I recover ubuntu 12.04 after failed hardware update?

  21. 21

    AngularJS/jQuery - Update Scope

  22. 22

    Update binding after server response / How do I selectively update bindings?

  23. 23

    How to update $scope variable value in AngularJs

  24. 24

    How do I get the html on the scope within a unit test to update after a button click event changes the active html?

  25. 25

    How do I make AngularJS update input values?

  26. 26

    How do I update an MKPointAnnotation image after downloading the right image?

  27. 27

    How do I programmatically calling Update IN after a shipment is confirmed?

  28. 28

    AngularJS directive scope variable doesnt update after reopening the window

  29. 29

    How do I fix broken boot after shim update?

HotTag

Archive