2015-05-01 71 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

相关问题