加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何将参数传递给控制器??中定义的方法,但是从Angularjs中的指令

发布时间:2020-12-17 17:31:16 所属栏目:安全 来源:网络整理
导读:我尝试将一个参数从指令传递给控制器??中定义的方法. 我使用隔离范围. 这是相关代码和Demo in Fiddle: HTML div ng-controller="MapCtrl" map id="map_canvas" call='callMe()'/map /div JS var module = angular.module('googleMap',[]);module.directive(
我尝试将一个参数从指令传递给控制器??中定义的方法.

我使用隔离范围.

这是相关代码和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 = undefined?应该是嘿

谢谢,

解决方法

您需要注释您的函数参数:

FIDDLE

视图:

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

指示:

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

这是explained in the docs(虽然有些模糊):

& 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}).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读