2

I've run into a problem in my app that I can't seem to find a solution to. My app collects form data, and I want one button at the bottom of the page to submit this form data and open up an sms message. I use ng-href with the sms: type and sanitzation in the compile provider to allow the button to open up an sms. This works without an ng-click on the same attribute, but when I attach an ng-click to that ng-href attribute, only the ng-click fires and the sms app is never opened.

Here's the relevant code

HTML

<a ng-href="sms:&body={{myMessage}}" ng-click="postData()">Invite</a>

AngularJS

$scope.myMesssage = "my message";

$scope.postData = function() {
  // prep data
  var myData = {
    "formData": $scope.formData,
  }

  // post data
  $http.post('/src/php/post.php', myData)
    .success(function(data, status, headers, config){
    })
    .error(function(data, status, headers, config) {
    });
}; 

The data is successfully submitted to the server, but no sms is opened. If I remove the ng-click, the sms opens. I saw this SO post about a similar issue and tried it to no avail. Any other ideas on how I can have ng-click and ng-href both execute on the same button?

If you would like any other information to help shed light on my issue, please ask. I stripped down my code to try and make my problem apparent, but I will happily provide more.

Community
  • 1
  • 1
Spencer F.
  • 29
  • 3
  • 2
    Can't you remove the ng-href and open the SMS in the postData function? – Mike Sav Dec 08 '15 at 20:01
  • Might need to prevent the default event from occurring. http://stackoverflow.com/questions/22504732/prioritize-ngclick-over-nghref-in-a-elements – stephen.vakil Dec 08 '15 at 20:02
  • I would use https://docs.angularjs.org/api/ng/service/$location in the post success function – erandac Dec 08 '15 at 20:06
  • 1
    Have you determined whether this behavior depends on the contents of the click handler or the `href` type? I have done a quick test [here](http://plnkr.co/edit/hYMf4fwlxc2nQne2i2CX?p=preview) and a similar setup (with `mailto` instead of `sms`) seems to work on desktop. FWIW, I also think that your `ng-href` should be `sms:?body={{myMessage}}` instead of `&`. – Igor Raush Dec 08 '15 at 20:10
  • @MikeSav How would I do that? I put $location.path("sms:&body=test"); into the function but that did not work. This format works when on an ng-href attribute. – Spencer F. Dec 08 '15 at 20:32
  • Shouldn't the & be ? instead, since it's the first parameter? Also have you tried using $window.location.href = 'sms:?body=test'; or $window.open('sms:?body=test'); rather than the $location object? – Mike Sav Dec 09 '15 at 09:40

0 Answers0