生成AngularJS模板字符串HTML字符串

生成AngularJS模板字符串HTML字符串

本文介绍了生成AngularJS模板字符串HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我angularjs模板包括NG重复等指令的字符串。我想编译它的控制器产生的结果HTML作为字符串。

I have angularjs template as a string including "ng-repeat" and other directives. I want to compile it in the controller to produce the result HTML as string.

这是我想要的角度应用示例:

Example on what I want to apply in Angular:

Input:
-------
var template = '<div ng-repeat="item in items">{{item.data}}</div>';

Output:
--------
var result = '<div>1</div><div>2</div><div>3</div><div>4</div>';

我想这在控制器我已经完成我试过如下:

I want this to be done in the controller I've and I tried the following:

var template = '<div ng-repeat="item in items">{{item.data}}</div>';
var linkFunction = $compile(template);
var result = linkFunction($scope);

console.log(result); // prints the template itself!

谢谢!

推荐答案

给这个一抡:

var template = angular.element('<div ng-repeat="item in items">{{item.data}}</div>');
var linkFunction = $compile(template);
var result = linkFunction($scope);
$scope.$apply();

console.log(template.html());

这篇关于生成AngularJS模板字符串HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:54