0

I know I'm not the first who has this problem but no solution over here helped me to solve it.

I've two controllers a ShowController and a CreateController. I want to broadcast from the ShowController and receive in the CreateCrontroller. To do so I'm using a BroadcastService. Everything works as expected. Due to a click-event the ShowController saves data to the service broadcast the event and the CreateController receives the event an the data. Dependent on the data I want to activate a tab one the view which was initiated by the CreateController. After setting the $scope variables in the on-method they are changing but the view is not updating itself.

I've tried to wrap the $broadcast and as well the $on methods in a $timestamp to force the $apply. I also tried the $apply after the $scope variables were set and I tried to set the $scope variables in an anonymous function in the $apply. Non of these solution worked for me. I'm obviously missing something.

EDIT The fiddle attached is working but there is not the problem. Both controllers have a parent controller. If I check the $scope in the child controllers the new data is added BUT if I checkt the parent controller the $$childTail with the child data did not changed. So the broadcast works. The problem is within the controller structure or scope-inheritance.

Why does the view listen to the parent controller even if the variable is not initiated and changed in the parent controller?

EDIT 2 As far as I tried to locate the issue I think I found one problem.If I try to broadcast from show controller to the create controller. The create controller isn't initialized yet. After I try it a second time the broadcast works. But still the variables doesn't change in the view! See this working plnkr to analyze the issue. I try to change the variable via broadcast from the show to the create controller. If you check the scope in the create controller the variable changed but in the parent scope it didn't.

    angular.module('module.service')
      .factory('SharedFactory',['$rootScope', '$timeout', function ($rootScope, $timeout) {
        var sharedService = {};

        sharedService.message = '';
        sharedService.data = {};

        sharedService.prepForBroadcast = function(event, data) {
          this.event = event;
          this.data = data;
          this.broadcastItem();
        };

        sharedService.broadcastItem = function() {
          $timeout(function() {$rootScope.$broadcast('updateShared');});
        };

        return sharedService;

      }]);


    // controller 1

     $scope.functionName = function(data) {
         // tried this as well with and without the $timeout
        $timeout(function() {
          SharedFactory.prepForBroadcast('bookResources', {data:data});
        }, 0);
         // ui-router changes state / view
         $timeout(function() {
           $state.go('create');
         }, 1);
        };

    // controller 2

    $rootScope.$on('updateShared', function() {
        // first attempt
      $timeout(function() {
        $scope.variable.data = SharedFactory.data; // updating in controller but not in view
       }, 100);
        // seconde attempt
       $scope.$apply(function () {
         $scope.variable.data = SharedFactory.data; // updating in controller but not in view
       });
    });

<ol>
    <li data-ng-repeat="phase in array">
        <span data-ng-repeat="com in phase.array">{{com.type}}
            <span data-ng-click="functionName(phase)">        {{com.var1}}/{{com.var1}}
            </span>
        </span>
    </li>
</ol>
Marc Radziwill
  • 175
  • 2
  • 12

1 Answers1

-1
  • check if the $on service is triggering.
  • check if SharedFactory.data has the updated value inside the $on function.
  • and you can type this in console to check the createControllers scope expand the scope object and see whether the variable that holds your data has the new data..

angular.element('[ng-controller=createController]').scope()

Divya MV
  • 2,021
  • 3
  • 31
  • 55
  • I think due to you last point I found the problem! They both have a parent controller. This is because of the ui-router. The parent controller is called projects. If I check the scope object of my create controller the new data is added. BUT if i check the scope object of the parent controller then the `$$childTail` the `$scope.variable` has not changed. Why does my view listen to the parent controller if i change and initiate the variable in the child (show, create) controllers? – Marc Radziwill Feb 06 '15 at 13:53
  • Can you create a working fiddle to analyse the issue if not solved yet – Divya MV Feb 09 '15 at 08:40
  • I'm still groping in the dark. The [fiddle](http://jsfiddle.net/rnfadkk0/23/) I just created is working. My code not. So there musst be another issue with my code. I'll trying to localize the issue a bit further. I'm going to keep this question up to date! – Marc Radziwill Feb 09 '15 at 12:41
  • I think I found the issue. pls see second edit above. – Marc Radziwill Feb 09 '15 at 16:33