Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same directive on multiple elements

I am trying to follow the example from this stackoverflow discussion on date formatting, it works very well for a page only has one single date field. However, if I have more than one date fields on the page, it seems like only the first date field/ng-model will get set, even other date fields are selected.

Below is the HTML template code:

    <div class="input-append" my-Datepickerloaded>
        <input type="text" ng-model="user.StartDate" my-Datepickerformater></input>
        <span class="add-on">
            <i data-time-icon="icon-time" data-date-icon="icon-calendar">
            </i>
        </span>
    </div>


    <div class="input-append" my-Datepickerloaded>
        <input type="text" ng-model="user.EndDate" my-Datepickerformater></input>
        <span class="add-on">
            <i data-time-icon="icon-time" data-date-icon="icon-calendar">
            </i>
        </span>
    </div>

And here is the directive code (myDatePickerformater):

return {
            require: '^ngModel',
            restrict: 'A',
            link: function (scope, elm, attrs, ctrl) {
                var moment = $window.moment,
                    dateFormat = 'MM/DD/YYYY';

                attrs.$observe('myDatepickerformater', function (newValue) {
                    ctrl.$modelValue = new Date(ctrl.$setViewValue);
                });

                ctrl.$formatters.unshift(function (modelValue) {
                    scope = scope;
                    if (!dateFormat || !modelValue) return '';
                    var retVal = moment(modelValue).format(dateFormat);
                    return retVal;
                });

                ctrl.$parsers.unshift(function (viewValue) {
                    scope = scope;
                    var date = moment(viewValue, dateFormat);
                    return (date && date.isValid() && date.year() > 1950) ? date.toDate() : "";
                });
            }
        };

I have tried to do a $scope.$watch on the models they bind to, it seems like even if I am changing the user.EndDate input field, it is always user.StartDate gets the change and user.EndDate remain untouched, even if the "input" field is displaying both fields correctly.

How do I make sure both fields will get their bind model updated correctly?

Thank you for helping.

like image 290
TheYouth Avatar asked Jul 12 '13 00:07

TheYouth


People also ask

Can we use multiple directives in angular?

You can not use two structural directives on the same element. You need to wrap your element in another one. It's advised to use ng-container since it wont be rendered in DOM.

Can we use two attribute directive in a single HTML element?

A component directive can be created multiple times, that is, every component in Angular will have a @Component decorator attached, while we cannot apply more than one structural directive to the same HTML element.

How many structural directives can you place on an element?

You may apply only one structural directive to an element.

Why We Use * with structural directives?

The magic behind the asterisk (*) The asterisk turns a attribute directive into a structural directive! The asterisk is syntactic sugar and just a short version to wrap an element in a <ng-template> . Every time we apply a directive with an asterisk…


1 Answers

You need to give your directive an isolated scope.

Right now, multiple instances of the directive are sharing the same scope, so updating your model is not working as expected.

Take a look at docs.angularjs.org/guide/directive

require: '^ngModel',
restrict: 'A',
scope: {
    ...
},
link: function (scope, elm, attrs, ctrl) {
    ....
like image 55
Alex Osborn Avatar answered Sep 20 '22 11:09

Alex Osborn