0

I am currently building one of my first custom HTML Element with an AngularJS directive. I want to customize the range input type from HTML5. So far so good, my first steps was made of success. But now I want to enhance the current state with a nested template. At the moment I have sth. like this:

.directive("mySlider", [function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            min: "@",
            max: "@",
            step: "@?",
            ngModel: "="
        },
        template: "<input class='my-slider' type='range'>",
        link: function ($scope, $element) {

            //some logic here
        }
    }
}]);

With that, if I use

<my-slider min="0" max="10" step="1" ng-model="sth"></my-slider>

I get a replacement like so:

<input class="my-slider" type="range" min="0" max="10" step="1" ng-model="sth">

Thats nice as the attributes from scope are written to the input type and can then be used directly.

But here is my problem: Now I want some output HTML like:

<div>

<label>blah</label>

<input class="my-slider" type="range" min="0" max="10" step="1" ng-model="sth">

</div>

So my input element should be nested into a div, maybe with additional element like a label or sth. But when I update my template and just go around nesting my input there, all the attributes from the directive scope are applied to the root element (the div in this case). I don't want them there but on the input element.

Is it possible and how can I achieve to apply certain attributes from the directives scope to different specific elements?

1 Answer 1

1

Change ngModel: "=" to sth: "=ngModel" to get the input model working. Use {{}} notation as you pointed out in the comments here for the attributes. And then make your parent attributes something like data-min so you don't have ugly source code.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello, thanks for your help. Seems to me that I'm just copying the attributes from the root element to the nested one? But I want to apply them ONLY to the the input and remove them from the parent div. Beside that it looks like I can't access the ng-model from my-slider on the input.
Because you have nothing in your scope named "sth", if you want that to work you need to change your isolate to sth: '=ngModel' which will give you a scope variable sth bi-directionally bound to the ng-model of the directive.
The var $input bit applies it ONLY to the input. If you want them removed from the parent then you lose the flexibility of the directive. It shouldn't matter since a "min" on a div doesn't do anything anyway. If you want you can prefix with data-min to make it less ugly. Data prefix is ignored by angular I think so the internal code doesn't change.
Hm, you are right about the data-attributes. I just saw this thread and it looks like it could solve my problem. Looks way cleaner to me. I didn't know I could link the scope variables to any element by {{}}: stackoverflow.com/a/15920467/2577116
Oh yea, that's way simpler. Do it that way, edited my answer.

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.