0

I've been mucking around with angularjs for the last few days, and have hit a brick wall now that I've started mucking about with javascript objects.

I'm trying to represent an attribute of an object in the view, and then after page load change that object and have the view change. I.e

Html

<div ng-controller = "MainCtrl">
    {{object.attribute}} //Expect A, and then B. Just get A
</div>

Javascript

app.controller('MainCtrl', ['$scope', function($scope) {    
  $scope.message_object = new object("A");

  $scope.replace_message = function(){ //Happens sometime after page load
    alert("Replacing...");
    $scope.object = new object("B");
  }

}]);

function object(attribute){
    this.attribute = attribute;
}

Don't know if I'm missing something, or just have a fundamental misunderstanding of the structure of the framework. Either way some help would be appreciated :) My current code is at:

http://plnkr.co/edit/3fF0zSyZaIvVsYk8dvWf?p=preview

it's designed to update the object when a key is pressed

1 Answer 1

1

You are changing the scope variable outside the angularjs context. Since you are making changes on a jquery event you need to call scope.$apply when changing the scope variable in your directive. See my updated plunker

http://plnkr.co/edit/h7NLFRdp4M7TcNIn1HeY?p=preview

This is the relevant code:

jQuery(document).on('keydown', function(e){

        scope.$apply(function(){
          scope.new_object();  
        });

     });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.