如何观看与NG绑定

编程入门 行业动态 更新时间:2024-10-27 22:20:38
本文介绍了如何观看与NG绑定-HTML创建NG-模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一些帮助与NG绑定-HTML创建的NG-模式。我在,我有与服务器的JSON文件的一些HTML和一些像这样的输入:

I need some help with an ng-model created with ng-bind-html. I have a JSON file in the server in which I have some html and some inputs like this:

* JSON

{ "test": { "1": { "question":"<span>1. something:</span>", "options":"<input type='radio' name='q1' ng-model='q.1' value='a'>a) op 1<br><input type='radio' name='q1' ng-model='q.1' value='b'>b) op 2<br><input type='radio' name='q1' ng-model='q.1' value='c'>c) op 3<br><input type='radio' name='q1' ng-model='q.1' value='d'>d) op 4<br><input type='radio' name='q1' ng-model='q.1' value='e'>e) op 5<br>", "answer":"c" }, "2": { ... } } }

然后在我的HTML文件我有这样的:

Then in my HTML file I have something like:

<div class="testContent"> <div class="test"> <div class="questions" ng-repeat="qs in questions" ng-show="questions.length"> <div ng-bind-html="qs.question"></div> <div class="options" ng-bind-html="qs.options"> </div> </div> </div> <br> <div class="nextBtn"> <a href="#test/6" class="btn btnNext" ng-click="save()"> continue ></a> </div> </div>

而在我的角度控制器我有Ajax调用的JSON文件:

And in my Angular controller I have the ajax call for the JSON file:

控制器:

.controller('testCtrl', ['$scope', '$http', 'myService', '$sce', function($scope, $http, myService, $sce, ){ $http.get(urls.url_otis).success(function(data, status){ angular.forEach(data.test, function(value, key){ var q = data.test[key]; q[key] = key; q.question = $sce.trustAsHtml(q.question); q.options = $sce.trustAsHtml(q.options); $scope.questions.push(q); }); }).error(function(data, status){console.log(status)}); }

该HTML填充,但我不能用$留意使用这种方法生成的模型(Q)。

The html is populated but I cannot use $watch for the model (q) generated with this approach.

我怎样才能观看$以这种方式创造了模型的变化?

How can I $watch for changes in the models created in this way?

在此先感谢...

推荐答案

您必须编译动态创建的元素,让角度了解他们。

You have to compile dynamically created elements to let angular know about them.

指令,它可以做到这一点可能看起来像这样的:

Directive which can do that may look like this one:

app.directive('compile',function($compile, $timeout){ return{ restrict:'A', link: function(scope,elem,attrs){ $timeout(function(){ $compile(elem.contents())(scope); }); } }; });

$超时用于运行编译功能,在 NG-绑定-HTML 完成其工作。

$timeout is used to run compile function, after ng-bind-html do its job.

现在你可以简单地把编译与 NG-绑定-HTML DIV的属性:

Now you can just simply put compile as attribute of div with ng-bind-html:

<div class="questions" ng-repeat="item in questions" ng-show="questions.length" > <div ng-bind-html="item.question"></div> <div compile class="options" ng-bind-html="item.options"></div> </div>

小提琴: jsfiddle/nZ89y/7/

更多推荐

如何观看与NG绑定

本文发布于:2023-11-06 17:38:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   NG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!