0

我最近改变了我的工厂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;
});

这个

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;
}]);

我这样做是出于 JS 缩小的原因。在我改变它之前,我在我的一个控制器中工作:

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

controllerComm.result自从我换了工厂后,现在又回来undefined了,我不知道为什么。有任何想法吗?

编辑

错误:

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) 
4

1 回答 1

1

尝试通过广播传递结果变量。

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

controllerComm 未在$on侦听器中定义,即使您可能将其注入到定义侦听器的控制器中。

$scope.$on('toggleVForum',function(evt,args){
    $scope.isVisible = args.result;
    ...
});
于 2013-08-29T19:10:17.073 回答