简体   繁体   English

如何从范围中的ngRepeat访问ngInit值

[英]How to Access a ngInit Value from ngRepeat in Scope

In my infoDisplay(member) I need to access the ng-init value specific to the div that I clicked on from the $scope and manipulate the value. 在我的infoDisplay(member)我需要访问特定于ng-init的值,该值是我在$ scope上单击的div并进行操作的。 How do I do so? 我该怎么做?

<div ng-repeat="member in memberData">
    <div class="card" ng-init="cardClicked = 'false'">
        <div ng-click="infoDisplay(member)">
        </div>
    </div>
</div>

The Javascript: Javascript:

$scope.infoDisplay = function (member) {
    if (settings.useApiInfo == 'false') {
        var url = settings.directoryUrl + member.Id;
        var openUrl = cordova.InAppBrowser.open(url, '_system', 'location = yes');
    }
    else {
        cardClicked = { 'true': 'false', 'false': 'true' }[cardClicked];
    }
}

The first part works, it is to open a url in my mobile app. 第一部分有效,它是在我的移动应用程序中打开一个URL。 I just need the second part but can't get it working. 我只需要第二部分,但无法正常工作。

What I would do is set the flag on the item itself: 我要做的是在项目本身上设置标志:

<div ng-repeat="member in memberData">
    <div class="card" ng-init="member.cardClicked = false">
        <div ng-click="infoDisplay(member)">
        </div>
    </div>
</div>

Then you can acces it in your controller: 然后,您可以在控制器中访问它:

$scope.infoDisplay = function(member) {
    var cardClicked = member.cardClicked;
}

If you think that your cardClicked shouldn't be on your member item, just track the values in an array: 如果您认为cardClicked不应出现在member项目中,则只需跟踪数组中的值即可:

<div ng-repeat="member in memberData">
    <div class="card" ng-init="cardsClicked[$index] = false">
        <div ng-click="infoDisplay(member, $index)">
        </div>
    </div>
</div>

Then you can acces it in your controller: 然后,您可以在控制器中访问它:

$scope.infoDisplay = function(member, index) {
    var cardClicked = $scope.cardsClicked[index];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM