2013-09-21 1 views
0

지시어 값에 따라 적절한 템플릿을 렌더링하는 지시문이 있습니다. 양식 파트를 렌더링합니다. 나는 그들을 모으고 어떤 변화에 대해 평가하고 싶다. 내가 지시 오 승/NG 변화를 사용하는 경우angularjs template ng-change not called

은, 모든 템플릿을 불러 NG 변화가 템플릿 안에되는 경우가 아닌 한, 잘 동작하는 것.

지침은 더 적은이 다음과 같습니다

myApp.directive('render', function() { 
    return function (scope, element, attrs) { 
     return attrs.$observe('parameters', function (value) { 
      var attributes, options, renderValue; 
      attributes = scope.$eval("{" + attrs.parameters + "}"); 
      renderValue = attrs.render; 
      if (renderValue === "input") { 
       return { 
        restrict: 'E', 
        replace: true, 
        template: element.html('<label for="' + element.text() + '">' + element.text() + ' </label><input name="' + element.text() + '" type=' + attributes.type + ' class="large-12 columns" ng-model="' + element.text() + '" ng-change="change()">') 
       }; 
      } 
     }) 
    } 

}); 

그것은 좀 더 다른 등등 선택, 라디오를 렌더링의 경우가 있습니다. 그러나 아이디어는 같습니다.

Here is the jsfiddle which shows my problem.

난에 어떤 도움을 주셔서 감사합니다.

편집 jsfiddle에

잘못된 페이스트, 지금 수정.

+0

당신이 무엇을 요구 확실하지. 분명히 게시 된 바이올린이 질문과 일치하지 않습니다. – zsong

+0

링크가 수정되었습니다. –

답변

1

흠 ngRepeat와 재사용 가능한 일반적인 구현에 대해 어떻게 생각하십니까?

http://jsfiddle.net/y3hUf/2/

var app = angular.module("app", []); 
app.directive("amount", function ($compile) { 
    return { 
     restrict: "E", 
     template: "<div class='amount'><input type='text' /></div>", 
     replace: true, 
     compile: function compile(tElement, tAttrs) { 
      return function (scope, iElement, iAttrs) { 
       var attributes = $(iElement).prop("attributes"); 
       var $input = $(iElement).find("input"); 
       $.each(attributes, function() { //loop over all attributes and copy them to the <input/> 
        if (this.name !== "class") { 
         $input.attr(this.name, this.value); 
        } 
       }); 
       $compile($input)(scope); //compile the input 
      }; 
     } 
    }; 
}); 

크레딧 : http://tech.pro/q/22/how-to-create-reusable-angularjs-directives-that-copy-existing-directives

는 희망이 도움이;)!