jamil
jamil

Reputation: 453

Calling a function through a button

How can I call this function through a button

module.controller('MyCtrl', function($scope, $cordovaSocialSharing) {
    $cordovaSocialSharing
    .share(message, subject, file, link) // Share via native share sheet
    .then(function(result) {
      // Success!
    }, function(err) {
      // An error occured. Show a message to the user
    });
})

Upvotes: 1

Views: 5040

Answers (3)

sheppe
sheppe

Reputation: 718

Use the ng-click attribute of the button element and bind it to a function in $scope that can call the cordova function.

In the javascript controller:

// Define a $scope visible function to call the cordova function.
$scope.shareIt = function(){
    $cordovaSocialSharing.share([add your parameters here])
    .then(function(result) {
        // Success!
    }, function(err) {
        // An error occurred. Show a message to the user
     }
    );;
}

In the HTML:

<form ng-controller="MyCtrl">
<button ng-click="shareIt()">Share This</button>
</form>

Here's a link to the AngularJS controller / button demo. http://plnkr.co/edit/?p=preview

Upvotes: -1

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You should wrap this function inside a $scope function & then call this function o button ng-click="socialSharing()"

Markup

<button ng-click="socialSharing()">Social Share</button>

Code

module.controller('MyCtrl', function($scope, $cordovaSocialSharing) {
    $scope.socialSharing = function() {
        $cordovaSocialSharing
            .share(message, subject, file, link) // Share via native share sheet
            .then(function(result) {
                // Success!
            }, function(err) {
                // An error occured. Show a message to the user
            });
    }

})

Upvotes: 3

domitall
domitall

Reputation: 655

wrap the function in a function declaration so that it's not immediately invoked when the controller is setup, then attach that function to the $scope

module.controller('MyCtrl', function($scope, $cordovaSocialSharing) {

 $scope.shareCordova = function() {
   $cordovaSocialSharing
   .share(message, subject, file, link) // Share via native share sheet
   .then(function(result) {
    // Success!
   }, function(err) {
     // An error occured. Show a message to the user
   });
 }

});

from your html template, use the ng-click directive to invoke the function.

<div ng-controller="MyCtrl">
      <div ng-click=socialSharing()></div>
</div>

Upvotes: 1

Related Questions