用Angular重写jQuery文件

Rewriting jQuery files with Angular

本文关键字:文件 jQuery 重写 Angular      更新时间:2023-09-26

我希望开始在我的应用程序中使用angularjs。我已经能够编写一些控制器,并从前端利用它们。我现在要做的是重构一些现有的.JS文件,这些文件是jQuery。例如:

原始message.js文件

define([ 'jquery' ], function($) {
'use strict';
return function(router) {
    router('*', function(ctx, next) {
        if (typeof console !== 'undefined');
            $('.message').on('click', function() {
                $(".message").show();               
            });     
        next();
    });
};
});

尝试新的message.js文件

define([ 'angular' ], function($) {
'use strict';
return function(router) {
    router('testpage.html', function(ctx, next) {
        if (typeof console !== 'undefined');
            $('.message').on('click', function() {
                $(".message").show();
                alert('its working)';
            });     
        next();
    });
};
});

然而,当我现在点击.message时,似乎什么都没发生。

有什么建议吗?

您必须使用angular.element()而不是$(),因为angular可以通过angular.element()访问jQlite。

此外,在jQlite中,通过类进行访问是不可能的。医生说:angular.element(element) // HTML string or DOMElement to be wrapped into jQuery.