简体   繁体   中英

How to use bind a element created by a directive into a model in Angular?

I want to model an input element with a object in controller, but the element is not a element in html. I have a directive and inside that I'm making that element( newElement in following snippets). 1.Directive:

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.View:

<div ng-custom-type  entity="currentEntity"></div>

When I check created input element in generated html I have ng-model='entity.name' in input element but it is not really modeled by my object, and when I change the input currentEntity.name doesn't change. How can I bind them together?

尝试这个:

newElement.attr("ng-model", "entity.name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM