我想用 Controller 中的对象对输入元素进行建模,但该元素不是 html 中的元素。我有一个指令,在其中我正在制作该元素(以下代码片段中的 newElement
)。
1.指令:
panel.directive('ngCustomType', function ($compile) {
return {
scope: {
entity: '='
},
link: function (scope, elem, attrs){
newElement = angular.element('<input type="text"/>');
newElement.attr("ng-model", "entity.name"); // I also tried newElement.attr("ng-model", "currentEntity.name"); but it didn't work
$compile(newElement)(scope);
elem.append(newElement);
}
2. Controller :
$scope.currentEntity = {name: ""};
3.查看:
<div ng-custom-type entity="currentEntity"></div>
当我检查生成的 html 中创建的输入元素时,输入元素中有 ng-model='entity.name'
但它并不是由我的对象真正建模的,当我更改输入时 currentEntity.name
不会改变。如何将它们绑定(bind)在一起?
最佳答案
试试这个:
newElement.attr("ng-model", "entity.name");
关于javascript - 如何在 Angular 中使用将指令创建的元素绑定(bind)到模型中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23511095/