2014-01-26 3 views
2

<select>을 모방하는 지시문을 만들었지 만 더 스타일링 할 수는 있지만 ng-model을 사용하여 구현하는 방법에 대한 정보는 찾을 수 없었습니다. 여기에 지침입니다 : 나는 <li>에 클릭 이벤트에서 NG 모델을 설정하는 방법Angularjs 지시어 변경 ng-model

.directive('customSelect', [function() { 
     return { 
      restrict:'EA', 
      require: "ngModel", 
      scope:{ 
       choices:"=", 
       selected:"=" 
      }, 
      template:'\ 
       <div class="custom-select">\ 
        <div class="label">{{ selected }}</div>\ 
        <ul>\ 
         <li data-ng-repeat="choice in choices" data-ng-click="ngModel = choice">{{ choice }}</li>\ 
        </ul>\ 
       </div>', 
      replace:true 
     } 
}]) 

?

답변

5

ngModel.$setViewValue을 시도해보십시오

app.directive('customSelect', [function() { 
     return { 
      restrict:'EA', 
      require: "?ngModel", 
      scope:{ 
       choices:"=", 
       selected:"@" 
      }, 
      link:function(scope,element, attrs,ngModel){ 
       scope.select = function (choice){ 
       ngModel.$setViewValue(choice); 
       } 
      }, 
      templateUrl:"template.html", 
      replace:true 
     } 
}]) 

템플릿 :

<div class="custom-select"> 
    <div class="label">{{ selected }}</div> 
    <ul> 
    <li data-ng-repeat="choice in choices" data-ng-click="select(choice)">{{ choice }}</li> 
    </ul> 
</div> 

DEMO

+2

데모가 완료되지 않은 (출력을 참조하는 항목을 클릭). – StarsSky

+1

@StarsSky : 아, 저장시 문제가 있습니다. 데모를 다시 만듭니다. –

+2

@StarsSky : 데모 URL을 업데이트했습니다. –