AngularJS - NG-绑定-HTML的不安全和NG-模型问题 [英] AngularJS - ng-bind-html-unsafe and ng-model Problems

查看:114
本文介绍了AngularJS - NG-绑定-HTML的不安全和NG-模型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下线在我的html:

I have the following line in my html:

< D​​IV NG绑定HTML的不安全=departmentConfig级=控制组>< / DIV>

和我使用了 $资源去检索HTML,指定的HTML $ scope.departmentConfig ,然后在视图完美更新。分配给 $ scope.departmentConfig HTML中包含表单元素, NG-模型在它的属性,但是当我改变这些输入元素的值,只要不更新 $范围模式可言。

and I use a $resource get to retrieve the HTML, assign the HTML to $scope.departmentConfig, and then the view is updated perfectly. The HTML that is assigned to $scope.departmentConfig contains form elements, with ng-model attributes in it, but when I change the values in these input elements, they don't update the $scope model at all.

这是我试过的基础上,很多其他互联网帖子,和它不工作:

This is what I have tried, based on a lot of other internet posts, and it isn't working:

$resource('resources/sources/departments/:mappedName', {
        mappedName:departmentKey
    }).get(function(departmentConfig) {
        // This will call another function that will build a chunk of HTML
        $scope.departmentConfig = $scope.buildDepartmentConfigHtml(departmentConfig);
        // This is my feeble attempt to access the element, and bootstrap it to include the items in the $scope model.
        var $departmentConfigContainer = $('#departmentConfig');
        angular.bootstrap($departmentConfigContainer, ['sourcemanager']);

我甚至看到了一些的jsfiddle例子,这似乎是工作,但我不是。我是不是叫引导太快?我也试过把一个 $观看 $ scope.departmentConfig 是这样的:

$scope.$watch('departmentConfig', function() {
    var $departmentConfigContainer = $('#departmentConfig');
    angular.bootstrap($departmentConfigContainer);
});

但它也不能工作。我敢打赌,有一个简单的解释这一点,我似乎无法得到与后页编译得到绑定到模型已加载 NG-模型输入元素。任何帮助是AP preciated,这是功能性的最后一块我需要让我的工作页面上。让我知道如果您需要了解我的配置,以及更多的信息。

but it didn't work either. I bet there is an easy explanation to this, I just can't seem to get the input elements with ng-model that are loaded after page compile to get bound to the model. Any help is appreciated, this is the last piece of functionality I need to get working on my page. Let me know if you need more information about my configuration as well.

所以,简单地说,我怎么能强迫DOM的部分重新编译后,我知道它已经被加载?

So, simply put, how can I force a section of the DOM to recompile after I know it has been loaded?

更新

下面是一个概括的jsfiddle我想做些什么: http://jsfiddle.net/j_snyder/ctyfg/ 。你会发现,房地产二,三不更新的模式,我呼吁引导在外DIV,希望将包括在模型绑定。这是我第一次张贴的jsfiddle,请让我知道,如果你不能看到的例子。

Here is a jsfiddle outlining what I would like to do: http://jsfiddle.net/j_snyder/ctyfg/. You will notice that property two and three don't update the model, and I am calling bootstrap on the outer div, hoping that will include those in the model binding. This is the first time I have posted to jsfiddle, please let me know if you can't see the example.

推荐答案

NG-绑定,HTML是普通的HTML制作,没有编制新的角度元素。

ng-bind-html is made for regular HTML, not compiling new angular elements.

您将使用 $编译服务

下面是你将如何修改当前的示例工作: http://jsfiddle.net/andytjoslin/ctyfg/21/ 。但这种方式结束是坏的,因为你需要做的DOM操作的控制器。

Here is how you would edit your current example to work: http://jsfiddle.net/andytjoslin/ctyfg/21/. But this way ends up being bad, because you have to do DOM manipulation in your controller.

您只需要创建一个指令,将基本做到你想要的NG-的bind-HTML做:的http:// jsfiddle.net/andytjoslin/ctyfg/22/

You just need to create a directive that will basically do what you wanted ng-bind-html to do: http://jsfiddle.net/andytjoslin/ctyfg/22/

这篇关于AngularJS - NG-绑定-HTML的不安全和NG-模型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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