本文介绍了Angular.js如何使用ng-click将参数从指令传递回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理注释过程的指令.我将其绑定到父控制器,如下所示:

I have a directive that handles the comments process. I'm binding it to a parent Controller, as followings:

<div commentable signin="signIn()"</div>

在评论模板中,我有多个登录提供程序按钮;因此,我需要将提供程序传递回父控制器.但是这样做给我带来了不确定性.

In the comment template I have multiple signin provider buttons; therefore I need to pass the provider back to the parent Controller. But doing this gives me undefined.

这是指令中的按钮:

<button type="button" ng-click="signin('app')">Sign In</button>

这是控制器功能:

$scope.signIn = function (provider) {
     console.log('clicked signin ' + provider);
}

如何将ng-click中的提供程序字符串绑定到函数中的Ctrl的提供程序?

How do can I bind the provider string from the ng-click to Ctrl's provider in the function?

推荐答案

您应该使用的工具.但是,请确保在控制器中声明了 signIn().因此:

What you have should work. Make sure signIn() is declared within your controller though. As such:

 function YourController($scope) {
     $scope.signIn = function (provider) {
        console.log('clicked signin ' + provider);
 }}

假设您的按钮位于控制器中,如下所示:

Assuming your button lives in the controller as such:

 <div ng-controller="YourController">
     <button type="button" ng-click="signIn('test')">Sign In</button>
 </div>

这是这项工作的小提琴: http://jsfiddle.net/tAsgt/

Here's a fiddle of this working: http://jsfiddle.net/tAsgt/

这篇关于Angular.js如何使用ng-click将参数从指令传递回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:38