Apply "datepicker" to element after angular call

Asked

Viewed 506 times

1

I’m having a problem, when loading an HTML block from the page via the angular, the elements that have the class ". datepicker", activate the jquery of the plugin "datepicker", however, as I noticed, the action that binds this block, is occurring after loading the elements.

I researched about, commented on the use of $watch and $on, we are calling the datepicker HTML / JS, as follows:

Call from the datepicker:

$('.datepicker').datepicker():

Element ( This element is inside an ng-controller and an ng-repeat ):

<input class="datepicker"  />

This input is inside an "ng-controller" and in an "ng-repeat", a directive is not being used, there is some way to trigger an event after loading the block?

3 answers

1

@helderburato

Try to do it this way:

$(document).on('click','.datepicker',function() {
   $(this).datepicker();
}

and see if it solves your problems :)

  • In reality, the problem is being, that this input, is inside an NG-REPEAT, and simply, does not work the datepicker inside it.

  • Then check out this OS English link: http://stackoverflow.com/questions/14099267/angular-ui-datepicker-in-ng-repeatworks-only-for-first-block

  • @helderburato or the way I said, or the link didn’t do you any good? If so, post an answer to your question to serve as a basis for others in the future.

0

In your HTML:

use ng-init!

<input ... class="datepicker" ng-init="initDatepicker()">

And in your Angularjs:

$scope.initDatepicker = function() {
    $('.datepicker').pickadate({ 
        selectMonths: true, 
        selectYears: 15
    });
}

0

Hello to integrate Datepicker in Angularjs the best way out is to create a custom Directive.

In the example below I created an angular app and includes a Directive named "incluiDatepicker" could be any name. The important thing is the call in the input. the name is separated by "-" e.g. includes-datapicker as input tag attribute. if Directive with a nameDirAng = my-dir-ang.

//
app.directive('incluiDatepicker', function () {
    return {
        require: 'ngModel',
        link: function (scope, elem) {
           elem.datepicker();
        }
    };
    });

<input type="text" id="nomedoId" incluir-datepicker>

I used it so it worked perfectly. the cool that you need me to keep calling the datepicker.

  • 1

    If your answer really solves the proposed problem, if possible, elaborate it better, after all the doubt of OP today, if solved with your answer, avoid duplication of content tomorrow. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.