呈现动态HTML(angularjs含量)AngularJS Ajax调用后的内容 [英] Rendering dynamic HTML(angularjs content) content after ajax call in AngularJS
问题描述
我是新来的角使得AJAX调用后,被卡住。我如何渲染/当你在DOM注入,这样我仍然可以使用AngularJs函数编译HTML内容。
I am new to Angular getting stuck after making ajax call. How do I render/compile the html content once you inject in DOM so that I can still use the AngularJs functions.
由于我的后端设置方式我得通过AJAX内容($ HTTP)。而我提出的应用,但不jQuery的。我试着$编译和$申请,但没有奏效。缺少什么我在这里。
Due to the way my backend is set up I have to get content via ajax ($http). And I am making the app without jQuery. I tried $compile and $apply but didn't work. What am I missing here.
我有code。在 http://jsfiddle.net/rexonms/RB7FQ/3/成立一>。我想第二个div的内容具有相同的属性作为第一个div。
I have the code set up at http://jsfiddle.net/rexonms/RB7FQ/3/ . I want the second div content to have the same properties as the first div.
<div ng-controller="MyCtrl" class="section">
<input ng-model="contentA">
<div>
And the input is: {{contentA}}
</div>
</div>
<div ng-controller="MyAjax" class="section">
<div id="dumpAjax">
{{ajaxData}}
</div>
<button ng-click=getajax()> Get Ajax</button>
</div>
SCRIPT
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
}
function MyAjax($scope){
var data = '<input ng-model="contentB">{{contentB}}';
$scope.getajax = function(){
$scope.ajaxData = data;
}
}
在此先感谢。
推荐答案
NG-结合HTML的不安全
不可用 1.2及更高版本verison角 ...
所以你应该使用 NG-绑定-HTML
这将创建一个绑定,将innerHTML的评估前pression到当前元素在<强的结果>安全的方法
so you should use ng-bind-html
which creates a binding that will innerHTML the result of evaluating the expression into the current element in a secure way.
$ sce.trustAsHtml
但这在您的字符串时变量不能被绑定,因为他们将无法编译...
using $scope
variable in your string make it unsafe, so you should use $sce.trustAsHtml
but this time variables in your string cannot be bind because they will be not compiled...
基本上你应该编译您以字符串绑定的变量。这里说到的自定义指令,您可以创建一个指令,它可以代替 NG-HTML绑定 ...
basically you should compile your string in order to bind your variables. Here comes custom directives you can create a directive which can replace with ng-html-bind...
编写延伸 NG-绑定-HTML
有一些额外的功能可以是一个解决办法...
Writing a custom directive which extends ng-bind-html
with some extra functions can be a solution...
这是我的 PLUNKER
和这里是你的更新的jsfiddle 我的解决方案...
and here is your updated JSFIDDLE with my solution...
这篇关于呈现动态HTML(angularjs含量)AngularJS Ajax调用后的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!