棱角分明,在指令中,增加了模板,NG模型中的元素 [英] angular, in directive, adding to the template an element with ng model
本文介绍了棱角分明,在指令中,增加了模板,NG模型中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图添加具有NG-模型中输入元素的指令中。
I'm trying to add an input element with ng-model inside a directive.
我的指令的链接功能:
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);
}
看来我需要调用$编译()结尾,但不知道怎么样。
it seems i need to call $compile() at the end, but have no idea how.
推荐答案
尝试
var a_input = angular.element($compile('<input type="text" ng-model="animals[0][' + i + '].name"/>')($scope))
elem_0.append(a_input);
这篇关于棱角分明,在指令中,增加了模板,NG模型中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文