1

I have a custom directive like this:

myText.html:

<div>
<label>{{label}}</label>
<input type="text" class="form-control" >
</div>

Javascript:

app.directive("myText", function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "shared/form/myText.html",
        scope : {
            label : "@",
        }
    };
});

I just want to wrap an input and handle a label as an attribute.

In my view i use the directive this way:

<my-text label="Name" ng-model="person.firstname"></my-text>

The problem is that ng-model is not binding the model to my input.

What is the correct way to achieve this result? Thanks

1 Answer 1

1

Put ng-model on the input and bind it to the isolate scope.

<my-text label=Name model=person.firstname></my-text>

<input type="text" class="form-control" ng-model=model>

return {
    restrict: 'E',
    replace: true,
    templateUrl: "shared/form/myText.html",
    scope : {
        label : "@",
        model: "=",
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Should I use a new attribute name such as 'model' or 'ngModel' as a best practice? I tried { model : "=ngModel"} and that worked...
@Rob80 I would use a name that makes sense and also try not to overload existing directive names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.