Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从AngularJS中的模板获取编译的html_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript 从AngularJS中的模板获取编译的html

Javascript 从AngularJS中的模板获取编译的html,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我必须承认,要找到一些关于在AngularJS中编译模板的基本且易于理解的指南并不容易。 交易如下: 在我的html主页面中,我有以下内容: <div> <div data-ng-include="'./views/testTemplate.html'"></div> </div> <div> <input type=button ng-click="func()" /> </div> $sco

我必须承认,要找到一些关于在AngularJS中编译模板的基本且易于理解的指南并不容易。 交易如下:

在我的html主页面中,我有以下内容:

<div>
    <div data-ng-include="'./views/testTemplate.html'"></div>
</div>
<div>
    <input type=button ng-click="func()" />
</div>
$scope.myname = 'max';  
$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
    $compile(newElem)(newScope);
    console.log('newElem');
    console.log(newElem);
});
var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
var compiledElem = $compile(newElem)(newScope);
console.log('compiledElem[0]');
console.log(compiledElem[0]);
我是我的javascript控制器我有:

<div>
    <div data-ng-include="'./views/testTemplate.html'"></div>
</div>
<div>
    <input type=button ng-click="func()" />
</div>
$scope.myname = 'max';  
$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
    $compile(newElem)(newScope);
    console.log('newElem');
    console.log(newElem);
});
var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
var compiledElem = $compile(newElem)(newScope);
console.log('compiledElem[0]');
console.log(compiledElem[0]);
现在,当我查看页面时,我看到文本“hello max”

Im我的javascript控制器我还有:

<div>
    <div data-ng-include="'./views/testTemplate.html'"></div>
</div>
<div>
    <input type=button ng-click="func()" />
</div>
$scope.myname = 'max';  
$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
    $compile(newElem)(newScope);
    console.log('newElem');
    console.log(newElem);
});
var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
var compiledElem = $compile(newElem)(newScope);
console.log('compiledElem[0]');
console.log(compiledElem[0]);
然后邮件正文包含以下内容(未编译模板):

然后邮件正文包含以下内容:

hello {{myname}}
[object HTMLElement]

因此,它们都没有在我要发送的邮件中显示html内容。我知道这不完全是最初的问题,但它是问题的一部分。

我认为变量'newElem'没有被$compile命令修改。它有一个您应该使用的返回值

var compiledElement = $compile(newElem)(newScope);
console.log('compiledElement');
console.log(compiledElement);

您缺少将
HTML
添加到
DOM
的功能

$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';

    //Append to DOM
    document.querySelector('#some-id').append($compile(newElem)(newScope));

    console.log('newElem');
    console.log(newElem);
});

使用
angular.element
创建模板,使用
$timeout
等待结束,然后使用
newElem.html()

请看一下编辑过的问题好吗?能否显示compiledElement变量中的内容?它是html对象的列表吗?对象有什么内容?实际上它只包含一个带有文本的div。我现在开始考虑是否可以调用“mailto”函数并向其发送一个html页面。这可能是另一个问题。我现在所能做的最好的事情就是设置一个超时,在这个超时中mailto函数从编译后的代码中获取html()。然而,在我客户端的邮件正文中,我只看到纯html代码。当我在寻找真正的html页面时。请你看看编辑过的问题好吗?@oderfla看到我的编辑,你需要等到
angular
编译完你的模板。