如何通过参数的方法在控制定义,但在Angularjs指令叫什么名字? [英] How to pass argument to method defined in controller but called from directive in Angularjs?

查看:131
本文介绍了如何通过参数的方法在控制定义,但在Angularjs指令叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从指令传递一个参数来控制中定义的方法。

I try to pass one parameter from directive to method defined in controller.

我用隔离范围。

下面是相关的code和演示在小提琴:

Here is relevant code and Demo in Fiddle:

HTML

 <div ng-controller="MapCtrl">
        <map id="map_canvas" call='callMe()'></map>
    </div>

JS

var module = angular.module('googleMap', []);

module.directive('map', function() {
    return {
        restrict: 'E',
        replace: true,
        scope:{
            callMe : '&call'
        },
        template: '<div></div>',
        link: function(scope, element, attrs) {
            console.log(element);

           /* ... */                
           scope.callMe('hey');

           /* ... */                                
        }
    };
});

function MapCtrl($scope) {

    $scope.callMe = function(val){
    alert(val);
    };

}

为什么我收到 VAL =未定义?它应该是

谢谢,

推荐答案

您需要注释的功能参数:

You need to annotate your function parameters:

FIDDLE

查看:

<map id="map_canvas" call='callMe(param)'></map>

指令:

scope.callMe({param: 'hey'});

这是在文档中解释(虽然有点含糊):

This is explained in the docs (although somewhat vaguely):

&安培;或放大器; attr指示 - 提供了一种在上下文中执行一个前pression
  父范围。如果没有指定ATTR名则属性名称
  被假设为相同的本地名称。给出的范围小部件定义:{
  localFn:'和; myAttr'},然后隔离scope属性localFn将指向
  一个函数包装为计数=计+值前pression。经常
  它是理想的通过前pression传递从隔离示波器数据
  并到母体范围,这可以通过使地图上的地方进行
  变量名和值进入前pression包装FN。例如,
  如果EX pression是增量(量),那么我们可以指定金额
  通过调用localFn作为localFn({量:22})值。

& or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

这篇关于如何通过参数的方法在控制定义,但在Angularjs指令叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆