2015-05-01 73 views
1

我如何可以調用這個函數通過一個按鈕通過一個按鈕調用函數

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 
    }); 
}) 

回答

1

包裹函數的函數聲明,以便它不會立即被調用時,控制器的設置,然後從你的HTML模板附上函數到$範圍

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 
    }); 
} 

}); 

,使用NG-點擊指令來調用該函數。

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

你應該換一個$範圍功能&在這個函數然後調用此函數o按鈕ng-click="socialSharing()"

標記

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

代碼

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 
      }); 
    } 

}) 
+1

'ng-click =「myFunctionName()」'括號括起來 – floribon

+0

@floribon感謝heads.I做了那些改變:) –

+0

你可以放一個完整的代碼示例嗎? – jamil

-1

使用按鈕元素的ng-click屬性,並將其綁定到$ scope中的函數,該函數可以調用cordova函數。

在JavaScript控制器:

// 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 
    } 
    );; 
} 

在HTML:

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

這裏給AngularJS控制器/按鈕演示的鏈接。 http://plnkr.co/edit/?p=preview

相關問題