Aravind S
Aravind S

Reputation: 535

AngularJS: open default mail application and populate to and subject via link

I'm trying to open a link using ng-href and as soon as I click on it, I need it to open an email address and populate its To, Subject and it's Body field. Can this be made possible?

I tried this,

<a ng-click=”sendMail($event)” href=”gmail.com” />

$scope.sendMail = function($event)
        {
            if($scope.showMailLink == true )
            {
                $event.preventDefault();
                window.location = $event.target.href;
                $window.open("mailto:[email protected]?subject=hello&body=fggf_self");
            }
        };

This is what I've tried and it isn't actually right. Is this a valid approach?

Upvotes: 3

Views: 3836

Answers (1)

lin
lin

Reputation: 18402

You should use location.href:

location.href = "mailto:[email protected]?subject=hello&body=fggf"

or target the current window:

$window.open("mailto:[email protected]?subject=hello&body=fggf", '_self');

- Example -

View

<div ng-controller="MyCtrl">
   <button ng-click="mailWithLocation()">
     Mail via location.href 
   </button>
   <button ng-click="mailWithWindowOpen()">
     Mail via $window.open() 
   </button>
</div>

AngularJS Application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, $window) {
    $scope.mailWithLocation = function () {
       location.href = "mailto:[email protected]?subject=hello&body=fggf"
    }

    $scope.mailWithWindowOpen = function () {
       $window.open("mailto:[email protected]?subject=hello&body=fggf", '_self');
    }
});

Upvotes: 7

Related Questions