我试图在一个指令中添加一个带有ng-model的输入元素.
@H_301_12@
尝试
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); }
似乎我需要调用$compile()在最后,但不知道如何.
var a_input = angular.element($compile('<input type="text" ng-model="animals[0][' + i + '].name"/>')($scope)) elem_0.append(a_input);