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

AngularJS – 将参数从ng-repeat传递给自定义指令中的控制器

发布时间:2020-12-17 17:14:38 所属栏目:安全 来源:网络整理
导读:我有一个自定义的AngularJS指令,它正在渲染一个ng-repeat.在这个ng-repeat中,我想将一个元素传递给一个函数.调用该函数,但item未定义. 重要提示:我让我的人员进行休息. //Inside the templateul class="list-group" data-ng-repeat="person in persons trac
我有一个自定义的AngularJS指令,它正在渲染一个ng-repeat.在这个ng-repeat中,我想将一个元素传递给一个函数.调用该函数,但item未定义.

重要提示:我让我的人员进行休息.

//Inside the template
<ul class="list-group" data-ng-repeat="person in persons track by $index">
<li>
... ng-click=fn(person) ...
</li>
</ul>

这是我喜欢传递这个人的功能.

//in outside controller    
$scope.showPerson = function (item) {
    console.log(item) // this is undefined
}

我的指令如下:

directive('custom',function () {
    return {
        restrict: 'E',scope: {
            persons: "=persons",fn: "&"
        },templateUrl: "/views/templates/persons.html"
    };
});

我这样称呼我的指令:

<custom persons="persons" fn="showPerson()"></custom>

任何想法为什么项目未定义?我唯一可以想象的是计时问题,因为该指令在数组之前被激活(来自其余的调用).

解决方法

传递给孤立范围的函数在接受参数时具有特殊性.对于初学者,在HTML中定义参数:

<custom persons="persons" fn="showPerson(x)"></custom>

请注意参数名称:x.现在从指令的模板中(这是特殊性):

<button ng-click="fn({x: person})" ...>

其中x是上面定义的参数的名称.

(编辑:李大同)

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

    推荐文章
      热点阅读