如何使用AngularJS发送HTML电子邮件?

时间:2014-09-14 15:56:42

标签: angularjs cordova

我正在使用一个cordova电子邮件插件,当我发送简单文本时。

但是,我试图改为使用模板来构建电子邮件的正文。 我认为$ compile可以解决这个问题所以我创建了一个on click函数:

    $scope.openShare = function (title,body) {
    var templateURL = "templates/roi-email.html";   
    $http.get(templateURL).success(function(data, status, headers, config) {
            var templateRendered = $compile( data )( $scope );

           if(window.plugins && window.plugins.emailComposer) {
                       window.plugins.emailComposer.showEmailComposerWithCallback(function(result) {
                    console.log("Response -> " + result);
                }, 
                title, // Subject
                templateRendered,                     // Body
                [],    // To
                null,                    // CC
                null,                    // BCC
                true,                   // isHTML
                null,                    // Attachments
                null);                   // Attachment Data
            }

但是,templateRendered不是字符串对象。我认为这是一个mg视图对象。

那么如何将'data'中的原始html转换为渲染字符串,即templateRendered传递给电子邮件编辑器?

2 个答案:

答案 0 :(得分:4)

使用角度元素,不要忘记触发使用范围的$ apply:

http://jsfiddle.net/coma/o63wvscn/

app.controller('Main', function($scope, $compile, $timeout) {

    var scope = angular.extend($scope.$new(), {
        user: {
            email: 'foo@foo.com'
        }
    });

    var email = angular.element('<div><p>{{ user.email }}</p></div>');
    $compile(email)(scope);

    scope.$apply();
    alert(email.html());
});

答案 1 :(得分:1)

$compile返回DOM元素;不是一个字符串。因此,您可以访问templateRendered.html()(jQuery)来获取内容。但是,正如the doc所说:

  

链接后,直到调用$ digest后才更新视图   这通常由Angular自动完成。

因此,在访问templateRendered.html()之前,您需要等待(或触发一个)当前摘要周期才能完成。

查看一个工作示例:http://plnkr.co/edit/cYsS1c7GbEVRP7RODHvx?p=preview

相关问题