将范围参数添加到ng-repeat内的onclick函数

时间:2015-05-29 13:20:04

标签: javascript html angularjs onclick angularjs-ng-repeat

我需要将参数传递给onclick内通过ng-repeat调用的函数:

 <!-- Sliding bar (bottom) -->
<div ng-show="currentSVG && currentLanguage && currentWidth && bannerHeight" pageslide ps-open="bottomBar" ps-side="bottom" ps-speed="0.3" align="center">
    <div id="related" class="content mThumbnailScroller" style="padding-top: 5px" ng-show="currentSVG && currentLanguage">
        <ul>
            <li ng-repeat="lastSVG in svgHistory track by $index">{{lastSVG}}<a href="javascript:void(0)" onclick="changeSVG({{lastSVG}})"><img src="../downloads/PNG/{{currentLanguage}}/{{lastSVG}}.png"  width="{{currentWidth * 0.1}}" height="{{bannerHeight * 0.65}}" title="{{lastSVG}}" /></a></li>
        </ul>
    </div>
</div>

如何将参数lastSVG提供给changeSVG()中的onclick功能?

changeSVG()不在控制器中。如果我设置参数,它可以正常工作。

此外,它不适用于ng-click而不是onclick

以下是changeSVG功能:(该功能不在控制器中)

function    changeSVG(svgName, defaultZoom){
    if (!defaultZoom)
        defaultZoom = 1;
    var scope = angular.element(document.getElementById('container')).scope();
    scope.currentSVG = svgName;
    scope.currentZoom = defaultZoom;
    if (scope.svgHistory.indexOf(svgName) === -1)
        if (scope.svgHistory.unshift(svgName) > 20)
            scope.svgHistory = scope.svgHistory.slice(0, 20);
    localStorage.setItem("svgHistory", JSON.stringify(scope.svgHistory));
    removeEmbed();
    var svgPath = "../SVG/" + scope.currentLanguage + "/" + svgName + ".svg";
    lastEmbed = createNewEmbed(svgPath, defaultZoom);
}

2 个答案:

答案 0 :(得分:0)

没有大括号的ng-click指令应该可以解决问题:

<a href="javascript:void(0)" ng-click="changeSVG(lastSVG)">

ng-click指令将查找名为lastSVG的作用域上的任何属性并找到它(因为ng-repeat为每次重复创建一个新作用域)

一个有效的插件示例:

http://plnkr.co/edit/W9WcWoNK9IrJWMjw8ryq?p=preview

答案 1 :(得分:0)

好吧,我必须更改我的代码并在控制器内部创建功能才能使其正常工作。 但我仍然想知道如果我们想用控制器外部的功能来实现这一点。