DOM操作中AngularJs [英] Dom manipulation in AngularJs

查看:101
本文介绍了DOM操作中AngularJs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果我们要操作DOM。我们应该这样做。
它是在一个控制器或指示或别的地方?
我在什么地方读到操纵控制器中的DOM应尽量避免。难道是正确的吗?

I wanted to know, If we want to manipulate the DOM. Where we should do that. Is it in a Controller or a directive or somewhere else ? I have read somewhere that manipulating the DOM in controller should be avoided. Is it correct?

推荐答案

您应该使用角JS指令 DOM 操作。 DOM 手法不应该在 控制器存在 服务 或其他任何地方,但在 指令

You should use Angular JS Directive for DOM manipulations. DOM Manipulations should not exist in Controllers, Services or anywhere else but in Directives.

一个 AngularJS 的更好的功能是框架的分离模型,视图和控制器的角色的能力。分离是不够干净,你不应该需要直接从控制器内部code操纵 DOM 。相反,控制器仅操纵模型,该模型通过影响数据绑定和指令的观点。最终的结果是一个干净,可测试的。

One of the nicer features of AngularJS is the framework's ability to separate the roles of the model, the view, and the controller. The separation is clean enough that you shouldn't need to manipulate the DOM directly from code inside the controller. Instead, the controller only manipulates the model, which influences the view through data bindings and directives. The end result is a clean and testable.

看看这个

视频 - 此视频教程包括操纵 DOM AngularJS 使用指令与链接功能。

Video - This video tutorial covers manipulating the DOM in AngularJS using directives with a link function.

这篇关于DOM操作中AngularJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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