2013-03-26 10 views
9

Próbuję dodać element wejściowy z ng-model wewnątrz dyrektywy.kątowa, w dyrektywie, dodając do szablonu element z modelem ng

my code

funkcja ogniwo mojego dyrektywy:

link: function (scope, element, attrs) { 
     var elem_0 = angular.element(element.children()[0]); 
     for (var i in scope.animals[0]) { 
      elem_0.append(angular.element('<span>' + scope.animals[0][i].id + '</span>')); 

      //this part doesn't work 
      var a_input = angular.element('<input type="text">'); 
      a_input.attr('ng-model', 'animals[0][' + i + '].name'); 
      //end 
      elem_0.append(a_input); 
     } 

wydaje Muszę zadzwonić $ kompilacji() na końcu, ale nie mam pojęcia jak to zrobić.

Odpowiedz

12

Spróbuj

var a_input = angular.element($compile('<input type="text" ng-model="animals[0][' + i + '].name"/>')($scope)) 
elem_0.append(a_input); 
+0

dzięki, działa dobrze. – Delremm

+1

http://jsfiddle.net/N2TDT/1/ na skrzypcach – Delremm

5

Robisz dyrektywa bardziej skomplikowana, niż to konieczne, przez ręcznie zapętlenie nad tablicami, kiedy można używać zagnieżdżonych ng-repeat w szablonie dyrektywy i niech kątowe zrobić tablica pętle:

angular.module("myApp", []) 
    .directive("myDirective", function() { 
    return { 
     restrict: 'EA',  
     replace: true, 
     scope: { 
      animals: '=animals' 
     }, 
     template: '<div ng-repeat="group in animals">'+ 
         '<span ng-repeat="animal in group">{{animal.id}}'+ 
          '<input type="text" ng-model="animal.name"/>'+ 
         '</span><hr>'+ 
        '</div>' 

    } 
}); 

DEMO: http://jsfiddle.net/Ajsy7/2/

Powiązane problemy