2013-11-28 3 views
6

각도 지침 데모 : 이제각도 지침 : 부모 범위 변수에 바인드

jsfiddle

<div ng-app="myApp"> 
<script> 
    function Contrl($scope){ 
     $scope.parval = 0; 
     $scope.items = [ 
      {id: 1, text: '1'}, 
      {id: 2, text: '2'}, 
      {id: 3, text: '3'} 
     ]; 
    } 
</script> 
<div ng-controller="Contrl"> 
    A: <input type="radio" value="1" ng-model="parval">1</input> 
    <input type="radio" value="2" ng-model="parval">2</input> 
    <input type="radio" value="3" ng-model="parval">3</input> 
    <item parval="parval" items="items"></item> 
</div> 

angular.module('myApp', []) 
.directive('item', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
      parval: '=', 
      items: '=' 
     }, 
     template: '<div>' + 
     'B: <span ng-repeat="i in items">' + 
       '<input value="{{i.id}}" type="radio" ng-model="parval">{{i.text}}</input>&nbsp;' + 
      '</span>' + 
     '</div>' 
    }; 
}); 

:
클릭 A1은 -> B1
클릭 A2를 선택 - > B2 선택됨

클릭 B1 - -> A1 변경되지
클릭 B2가> A2 내가 원하는


을 변경하지 :
클릭 A1을 -> B1 선택
클릭 A2를 -> B2
선택
클릭 B1을 -> A1 선택한
클릭 B2 -> A2

어떻게?

답변

4

ng-repeat는 새로운 범위를 만들기 때문에 대신 리터럴 표기법을 사용하지 말아야하는 프리미티브를 사용하고 있습니다. 아래의 코드는 아이 컴퍼넌트에 부모로부터 값을 전달하는 (NG-모델 = "$의 parent.parval")

<div ng-controller="Contrl"> 
    A: <input type="radio" value="1" ng-model="parval.value">1</input> 
    <input type="radio" value="2" ng-model="parval.value">2</input> 
    <input type="radio" value="3" ng-model="parval.value">3</input> 
    <item parval="parval" items="items"></item> 
</div> 
    <script> 
     function Contrl($scope) { 
      $scope.parval = { value: 0 }; 
      $scope.items = [ 
       { id: 1, text: '1' }, 
       { id: 2, text: '2' }, 
       { id: 3, text: '3' } 
      ]; 
     } 
     angular.module('myApp', []) 
.directive('item', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
      parval: '=', 
      items: '=' 
     }, 
     template: '<div>' + 
     'B: <span ng-repeat="i in items">' + 
       '<input value="{{i.id}}" type="radio" ng-model="parval.value">{{i.text}}</input>&nbsp;' + 
      '</span>' + 
     '</div>' 
    }; 
}); 
    </script> 
+0

을 문제를 해결할 수 우리는 바인딩을 사용할 수 있습니다 : { 텍스트 : '<' },하지만 부모와 자식 사이에해야 할 일은 무엇입니까? (tet는 부모에게서 아이에게 보여줄 것이다) – cracker

5

한 가지 방법은 $ 부모 을 사용하는 것입니다

angular.module('myApp', []) 
.directive('item', function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     template: '<div>' + 
     'B: <span ng-repeat="i in items">' + 
       '<input value="{{i.id}}" type="radio" ng-model="$parent.parval">{{i.text}}</input>&nbsp;' + 
      '</span>' + 
     '</div>' 
    }; 
});