2013-09-01 6 views
2

나는 테스트를위한 '대화'지시어를 쓸 수 있지만 나는 요소가 testDirective가 추가되는 경우 지침'지시자'에서 '컨트롤러'의 값을 업데이트하는 방법은 무엇입니까?

app.controller('testController',function($scope){ 
    $scope.testValue = 'testing'; 
}); 

app.directive('testDirectvie',function(){ 
    return function(scope,element,attr){ 
     // this func will open the modal window, 
     // so how can I can the testValue from controller?Thx all 
    }; 
}); 

답변

0

에서 컨트롤러에서 값을 업데이트하는 방법을 모르는에 (testController 안에 예를 들어 <div ng-controller="testController"><span test-directive=""></span></div>을) 다음 지시문에서 직접 범위에 액세스하면됩니다.

app.directive('testDirective',function(){ 
    return function(scope,element,attr){ 
     scope.testValue = "New value"; 
    }; 
}); 

예 - http://plnkr.co/edit/lN05YDr4bckrBRPiHyT7?p=preview

testDirective 그래도 testController의 범위를 벗어난 경우, 당신은 당신이 데이터를 공유하기 위해 모두에 삽입 할 수있는 공유 서비스를 작성해야합니다.

컨트롤러와 지침을 모두가 공유하는 서비스의 예 - http://plnkr.co/edit/lN05YDr4bckrBRPiHyT7?p=preview

+0

그래, 내 지시 범위가 testController의 부족, 그래서 공유 서비스를 작성하는 방법을 당신은 다른 사람 "방송"또는 의미 덕분에 –

+0

와 경우를? 지시문에 몇 가지 매개 변수를 입력하고 싶습니다. 어떻게해야합니까? –

+0

글쎄, 방송은 이벤트를위한 것보다 많고, 서비스는 데이터를 공유하기위한 것입니다. 서비스가 어떻게 작동하는지 예를 들어 답을 업데이트했습니다. – mikel

관련 문제