0

I have a main controller EventCtrl that sets a formData variable on its scope.

This controller has a setEventObject method that overrides the formData object.

My purpose is to be able to call this method from child directives and apply the new variable value recursively to every scopes.

Here is the controller and its setEventObject function :

app.controller('EventCtrl', function($scope) {
  $scope.formData = {
    id: 1,
    name: 'tennis lesson'
  }
  $scope.events = [
    {id: 1, name: 'tennis lesson'}, 
    {id: 2, name: 'golf lesson'}, 
    {id: 3, name: 'handball lesson'}
  ];

  this.setEventObject = function(eventId) {
    angular.forEach($scope.events, function(elem) {
      if (elem.id == eventId) {
        $scope.formData = {
          id: elem.id,
          name: elem.name
        };
      }
    });
  }
});

Here is the associated Plunkr, on clicks on links, it should set the form input and reset the formData variable.

Thanks for helping !

1
  • You should look at using $broadcast and $on, see the docs Commented Oct 5, 2014 at 17:34

1 Answer 1

1

You are using element.bind('click' to listen to click. This is jQuery, and these changes are outside angular world. You need to tell angular about your change.

replace eventCtrl.setEventObject(scope.event.id); line with

scope.$apply(function () {
    eventCtrl.setEventObject(scope.event.id);
});

I tried the above change in your plunker and it works as expected.

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.