与DOM操作处理 - AngularJS [英] Dealing with DOM Manipulations - AngularJS

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

问题描述

当我使用jQuery执行DOM操作(添加新的HTML),AngularJS不自动检测变量在新的HTML和与他们的价值观取代他们。例如:

When I perform DOM manipulation (add new HTML) using jQuery, AngularJS doesn't automatically detect variables in the new HTML and replace them with their values. For example:

$scope.showSummary = function($event){

    $($event.currentTarget).html("<div>{{row}}</div>");

};

这是一个简单的例子,但在元素改变了HTML(此功能是通过NG-点击调用),输出后,仍然是{{行}},而不是什么的应该意味着在上下文/范围。

This is a simple example, but after changing the html in the element (this function was called by ng-click), the output it still "{{row}}" instead of what row should mean in the context/scope.

任何帮助是极大AP preciated!

Any help is greatly appreciated!

推荐答案

您必须注入 $编译 http://docs.angularjs.org/api/ng.$compile ),并使用它,棱角分明知道新的HTML。

You have to inject $compile (http://docs.angularjs.org/api/ng.$compile) and use it so angular knows about the new html.

$编译('&LT; D​​IV&GT; {{行}}&LT; / DIV')($范围).appendTo($ event.currentTarget);

然而,在后角皱着眉头做DOM操作在控制器。您希望您的控制器来处理的业务和你的意见来处理视图。

However, it is frowned upon in angular to do DOM manipulation in your controllers. You want your controllers to handle business and your views to handle the view.

尝试指令,做你想做的。 http://docs.angularjs.org/guide/directive

Try a directive to do what you want. http://docs.angularjs.org/guide/directive

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

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