2013-08-29 4 views
0

는 최근 내가 JS의 축소를 이유로 이런 짓을 본각도 JS 공장과 변수 액세스

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

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

에서 내 공장 을 변경했습니다. 내가 그것을 변경하기 전에, 나는 내 컨트롤러 중 하나에서이 작업했다 : 내 공장을 변경하기 때문에

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

답변

1

는 방송과 결과 변수를 전달하십시오.

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

controllerComm는 당신이 리스너가 정의 된 컨트롤러로 주입 할 수있다하더라도 $에 청취자에 정의되어 있지 않습니다.

$scope.$on('toggleVForum',function(evt,args){ 
    $scope.isVisible = args.result; 
    ... 
}); 
+0

그랬습니다. 시도했지만, 첫 번째 주장은 이벤트였습니다. '$ to ('toggleVForum', function (args) {'하지만 그게 작동하지 않는 이유를 설명하고 있습니다.) 나는 또 다른 문제를 겪고 있습니다. 두 번째로, '.. blah blah는 아무런 메소드도 없다'라는 오류가 발생합니다. – Ronnie

+0

두 번째 호출은 어디에서 이루어 집니까? –

+0

기본적으로 저는 두 개의 컨트롤러가 있습니다. 첫 번째 호출은'controllerComm.prepBroadcast (true)'를 호출하여 부울을 토글합니다 두 번째 컨트롤러는'controllerComm.prepBroadcast (false)'를 호출하여 사용자가 클릭 할 때 다시 토글합니다. 두 번째 컨트롤러가'controllerComm.prepBroadcast (false) '를 호출하면 오류가 추가됩니다. – Ronnie