Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a jQuery Plugin in an AngularJS View

I am working on my first AngularJS project. I love it so far, but I'm hung up now.

My project is structured so that I've got a single ng-view in my index.html that is populated with a separate HTML template/partial based on the current route.

In one of the partials I'd like to use the jQuery DateFormat plugin to format a SQLite datetime string that is parsed into the template/partial using an angular expression:

{{ find.addDate }}

I've included the plugin with a script tag in index.html and I thought from there it would be as simple as doing something like this in my template/partial:

{{ $.format.date(find.addDate, "dd/MM/yyyy") }}

Or maybe:

{{ angular.element.format.date(find.addDate, "dd/MM/yyyy") }}

That's not working (I'm sure it's obvious why to some of you), but there's no error in the console and I'm pretty stumped on how to approach it. I've done some Googling with terms like 'third party scripts in angular' or 'jquery plugin angularjs', etc. but can't find recent code examples on how to accomplish this the right way.

I did find some older code using angular.widget, but it appears that's been deprecated in the 1.0+ releases. I think I need to use a directive, but I can't figure it out exactly.

I'm really hoping for an explanation or simple example please. Thanks a lot!

like image 427
Trae Avatar asked Jul 08 '12 02:07

Trae


People also ask

Can we use jQuery plugin in Angular?

Most beginners prefer to write an Angular wrapper for the jQuery plugin. In this, all we have to do is apply an Angular Attribute Directive appToolbarBoys on the toolbarBtn button and instantiate the jQuery plugin when the directive is initialized.

Can we use jQuery in AngularJS?

Does AngularJS use the jQuery library? Yes, AngularJS can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, AngularJS falls back to its own implementation of the subset of jQuery that we call jQLite.

Where do I put jQuery plugins?

How to use Plugins. To make a plug-in's methods available to us, we include plug-in file very similar to jQuery library file in the <head> of the document. We must ensure that it appears after the main jQuery source file, and before our custom JavaScript code.


1 Answers

The problem is when you do {{expression}} in a template, it evaluates it on the current scope.

For example, if you have this controller:

function MyCtrl($scope) {
    $scope.find = {
        addDate: 2030
    };
}

{{find.addDate}} would in reality look for $scope.find.addDate and evaluate it (in this case returning 2030).

So when you try to do {{ $.format.date(find.addDate, "dd/MM/yyyy") }}, angular looks for $scope.$.format.date(find.addDate, "dd/MM/yyyy"), which doesn't exist.

Try something like this instead, using a function in your controller:

   function MyCtrl($scope) {
        $scope.find = {
            addDate: 2030
        };
        $scope.formatDate = function(input, format) {
            return $.format.date(input, format);
        }
    }

And then you can do in your html you can do: {{formatDate(find.addDate, "dd/MM/yyyy")}}.

You could also evaluate it as a filter in your markup, since it just takes an input and changes it: {{find.addDate | formatDate:"dd/MM/yyyy"}}

like image 50
Andrew Joslin Avatar answered Sep 24 '22 07:09

Andrew Joslin