0

I recently changed my factory FROM

app.factory('controllerComm', function($rootScope)
{
  var showVforum    = {};
  showVforum.result = false;
  showVforum.prepBroadcast = function(val)
  {
    this.result = val;
    this.broadcastVal();
  }

  showVforum.broadcastVal = function()
  {
    $rootScope.$broadcast('toggleVforum')
  }
  return showVforum;
});

To THIS

app.factory('controllerComm', ['$rootScope', function($rootScope)
{
  var showVforum    = {};
  showVforum.result = false;
  showVforum.prepBroadcast = function(val)
  {
    this.result = val;
    this.broadcastVal();
  }

  showVforum.broadcastVal = function()
  {
    $rootScope.$broadcast('toggleVforum')
  }
  return showVforum;
}]);

I did this for JS minification reasons. Before I changed it, I had this working in one of my controllers:

$scope.$on('toggleVforum', function()
{
  $scope.isVisible = controllerComm.result;
  $('#vforum').verticalAlign();
  player.play();
});

controllerComm.result is now returning undefined since I changed my factory and I cannot figure out why. Any ideas?

edit

the error:

TypeError: Object function e(e,f,i){var j=c.defer(),k=j.promise,l=y(i)&&!i,f=a.defer(function(){try{j.resolve(e())}catch(a){j.reject(a),d(a)}l||b.$apply()},f),i=function(){delete g[k.$$timeoutId]};
k.$$timeoutId=f;g[f]=j;k.then(i,i);return k} has no method 'prepBroadcast'
    at Object.$scope.hideVforum (http://localhost/aventos/resources/js/aventos.js:645:20)
    at http://localhost/aventos/resources/js/angular.min.js:72:251
    at http://localhost/aventos/resources/js/angular.min.js:144:140
    at Object.e.$eval (http://localhost/aventos/resources/js/angular.min.js:88:347)
    at Object.e.$apply (http://localhost/aventos/resources/js/angular.min.js:88:454)
    at HTMLButtonElement.<anonymous> (http://localhost/aventos/resources/js/angular.min.js:144:122)
    at HTMLButtonElement.x.event.dispatch (http://localhost/aventos/resources/js/jquery-1.10.2.min.js:5:14129)
    at HTMLButtonElement.v.handle (http://localhost/aventos/resources/js/jquery-1.10.2.min.js:5:10866) 

1 Answer 1

1

Try passing the result variable with the broadcast.

$rootScope.$broadcast('toggleVForum',{result: this.result});

controllerComm is not defined in the $on listener even though you may be injecting it into the controller where the listener is defined.

$scope.$on('toggleVForum',function(evt,args){
    $scope.isVisible = args.result;
    ...
});
Sign up to request clarification or add additional context in comments.

9 Comments

That did it, I was trying that but I missed the first argument was event. I was doing $scope.$on('toggleVForum',function(args){ but that explains why it wasn't working. I am running into another issue though. When I call controllerComm.prepBroadcast(false) the second time, I get an error saying '..blah blah has no method prepBroadcast`
Where is the 2nd call being made from?
Basically I have two controllers, the first calls controllerComm.prepBroadcast(true) which toggles a boolean in the second controller. The second controller then calls controllerComm.prepBroadcast(false) which toggles it yet again (when the user clicks). I added the error when my second controller calls controllerComm.prepBroadcast(false)
If the service controllerComm is being injected into both controllers, then the method prepBroadcast should be available.
Your second controller's parameter list is wrong. You're injecting $q but don't add it in the controller's function parameter list. controllerComm is actually receiving $timeout's definition, that's why you see it as a big messy function.
|

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.