2014-03-25 5 views
0

첫 번째 지시문 중 하나를 쓰고 있는데 지시문의 범위에 일부 스텁 데이터를 만들고 싶습니다. 여기격리 된 범위 지시문의 로컬 데이터

지시문입니다 :

angular.fraskDirectives.directive("recipientsSelector", [ 

    function() { 
     return { 
      restrict: "E", 
      replace: true, 
      scope: { 
      recipients: "=", 
      theText: "test", 
      users: [{ 
       username: "aaa" 
      }, { 
       username: "aab" 
      }, { 
       username: "abb" 
      }, { 
       username: "bbb" 
      }, { 
       username: "bbc" 
      }] 
     }, 
     templateUrl: "partials/recipientsSelector.html" 
    }; 
    } 
]); 

을 여기 템플릿입니다 :

<div> 
<div contenteditable> 
    <p>{{theText}}</p> 
</div> 

<ul class="suggestions"> 
    <li ng-repeat="user in users"> 
     {{user.username}} 
    </li> 
</ul> 

그러나도 "텍스트"필드 나 사용자 필드가 바인딩되지 않습니다. 나는 ul이 비어 있고 {{text}}이 페이지에서 "{{text}}"처럼 보입니다.

내 지시문의 분리 된 범위에서 로컬 데이터를 어떻게 만들 수 있습니까?

감사

답변

1

,이 지침 이외의 어떤 것에도 구속되지 않은 경우에만 "로컬"데이터 범위를 채울 아래와 같이 링크 기능을 사용하려면 :

angular.fraskDirectives.directive("recipientsSelector", [ function() { 
    return { 
     restrict: "E", 
     replace: true, 
     scope: { 
      recipients: "=" // note that theText and users are missing 
     }, 
     templateUrl: "partials/recipientsSelector.html", 
     link: function (scope, elem, attrs) { 
      scope.theText = "test"; 
      scope.users = [{ 
       username: "aaa" 
      }, { 
       username: "aab" 
      }, { 
       username: "abb" 
      }, { 
       username: "bbb" 
      }, { 
       username: "bbc" 
      }]; 
     } 
    }; 
}]); 

대안은을 할당하는 것 컨트롤러를 지시문에 추가하고 컨트롤러의 범위를 채 웁니다.

+0

그리고 어떻게 텍스트와 사용자 모두 양방향 바인딩을 추가 할 수 있습니까? – earsonheart

+0

양방향 바인딩을 원할 경우 아래의 scope 속성에있는받는 사람에 대해'theText : "="'및'users : "="'를 replace (여기서 내가 코멘트를 작성한 곳)에 넣으십시오. 변수를 다시 채우려면 링크 기능이나 컨트롤러를 사용하십시오. –

관련 문제