在 AngularJS 中使用 jQuery 是一个好习惯吗? [英] Is that a good practice to use jQuery with AngularJS?

查看:29
本文介绍了在 AngularJS 中使用 jQuery 是一个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多年来一直在使用 jQuery.现在,我想学习一些新的东西来构建单页网站.我选择了 AngularJS.

I have been using jQuery for years. Now, I would like to learn something new to built single page websites. I have chosen AngularJS.

目前我想知道将 jQuery 与 AngularJS 一起使用是否是个好主意?

At the moment I am wondering if it is a good idea to use jQuery with AngularJS ?

示例.

我在 AngularJS 控制器中有类似的东西:

I have something like that inside AngularJS controller:

$http.post('ajax/gettimeline', { page: page }).success(function (data) {
        $rootScope.loading = false;


            // Here I need to built dynamic html with getting and setting
            // dynamic elements size, positions, change their properties, etc.

        }
    });

使用 jQuery 构建那些动态 html 元素是一项简单的任务.但是使用 AngularJS 看起来会很痛苦.

Building those dynamic html elements with jQuery is an easy task. But with AngularJS looks like it will be painful.

将不胜感激任何建议.

谢谢

推荐答案

不,这不是一个好主意.您在 Angular 应用程序中使用 jQuery 编码实践,这会让您头疼——这一次是因为这些实践违背了 Angular 精神,而且还因为您无法获得 Angular 的好处.

No, it is not a good idea. You are using jQuery coding practices in an Angular app and this will cause you headaches - for once because those practices go against the Angular spirit, and also because you won't reap the benefits of Angular.

Angular 开发中最重要的部分之一是:永远不要从控制器操作 DOM.这在 jQuery 中很好,但在 Angular 中,你应该修改你的模型并让 Angular 渲染它.Angular 有一个内置的模板机制,你应该用它来加载 HTML 片段.如果您需要动态创建 HTML,您应该在指令中这样做.其他一切都违反了关注点分离"原则.

One of the most important parts in Angular development is: you shouldn't manipulate the DOM from your controllers, ever. This is fine in jQuery, but in Angular, you should modify your model and let Angular render it. Angular has a templating mechanism built in which you should use to load HTML fragments. If you need to create HTML dynamically, you should do so in a directive. Everything else violates the "separation of concerns" principle.

如果您使用 jQuery 或本机 DOM 方法来操作输入设置或修改 DOM,那么您将自己准备与 Angular 进行斗争,这将导致许多难以调试的错误.Angular 的要点之一是 双向databinding 如果您坚持自己将数据绑定到 HTML,这不是一个有用的功能.也许像mustache 或knockout 这样更简单的模板库之一会更符合您的喜好?

If you use jQuery or native DOM methods to manipulate input settings or modify the DOM, you set up yourself for a fight with Angular over it which will result in a lot of hard-to debug errors. One of the main points of Angular is two-way databinding which is not a useful feature if you insist on binding data to HTML on your own. Maybe one of the simpler templating libraries like moustache or knockout would be more to your liking?

要获得 Angular 的好处意味着放弃一些尝试过的真实实践并学习内置指令,尤其是 ng-repeatng-ifng-classng-clickng-change 可能还有 Angular UI.

To reap the benefits of Angular means letting go some tried and true practices and learning about the built-in directives, especially ng-repeat, ng-if, ng-class, ng-click and ng-change and maybe something like Angular UI.

这篇关于在 AngularJS 中使用 jQuery 是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆