最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using a jQuery Plugin in an AngularJS View - Stack Overflow

programmeradmin1浏览0评论

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 acplish 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!

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 acplish 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!

Share Improve this question asked Jul 8, 2012 at 2:25 TraeTrae 6671 gold badge10 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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"}}

发布评论

评论列表(0)

  1. 暂无评论