如何在不更改窗口位置的情况下触发AngularJS控制器中的mailto

时间:2017-09-21 14:20:37

标签: angularjs mailto

我有一个角度控制器可以触发" mailto"单击邮件图标时链接

angular
  .module('app')
  .component('header', {
    template: '<a href="" ng-click="headerCtrl.mailTo()"><i class="fa fa-envelope" aria-hidden="true"></i></a>',
    controller: HeaderController,
    controllerAs: 'headerCtrl'
  });

function HeaderController() {
  var vm = this;

  vm.sendMail = sendMail;

  function sendMail(response) {
    var email = 'test@mail.com';
    var subject = 'email%20subject';
    var body = 'email%20body';
    vm.mailToUri = 'mailto:' + email + '?subject=' + subject + '&body=' + body;
    $window.location = vm.mailToUri;
  }
}

不幸的是,这与在浏览器关闭时调用注销网址的指令在同一个应用程序中运行

angular
  .module('app')
  .directive('onCloseLogout', onCloseLogout);

function onCloseLogout($http, $window) {
  return {
    restrict: 'AE',
    link: function () {
      $window.onbeforeunload = function () {
        $http.get('/logout');
      };
    }
  };
}

因此,单击电子邮件链接时,将触发onCloseLogout指令,并且用户已注销。

是否有其他方式以角度打开电子邮件,或者在浏览器关闭时向注销网址发送获取请求?

1 个答案:

答案 0 :(得分:0)

原来我已经过度设计了解决方案。我将mailToUri作为ng-href属性添加到锚标记中,现在一切正常

angular
  .module('app')
  .component('header', {
    template: '<a ng-href="{{headerCtrl.mailToUri}}"><i class="fa fa-envelope" aria-hidden="true"></i></a>',
    controller: HeaderController,
    controllerAs: 'headerCtrl'
  });

function HeaderController() {
  var vm = this;

  var email = 'test@mail.com';
  var subject = 'email%20subject';
  var body = 'email%20body';
  vm.mailToUri = 'mailto:' + email + '?subject=' + subject + '&body=' + body;
}