簡體   English   中英

從模板到函數的訪問對象屬性 - Angular

[英]Access object properties from template to function - Angular

簡單地說,我有一系列對象,[圖像],我在Ng-repeat上顯示完美。 當我單擊一個div時,我想看到模板中單擊的特定圖像的“id”屬性。

控制器中的功能:

angular.forEach(value, function (value, key) {
    $scope.images.push({
        id: i,
        src: ("data:image/jpeg;base64," + value)
    });
    i = i + 1;
}

$scope.seeOne = function () {
    console.log(image.id);
}

我的模板:

<div id="galscrolldiv" ng-repeat="image in images">
    <div ng-click="seeOne()">
        <img ng-src="{{images[$index].src}}" width="100%" />
    </div>
</div>

從視圖/模板發送image作為參數。 然后,您可以訪問所單擊圖像的圖像屬性。

你已經錯過了)forEach

控制器:

angular.forEach(value, function (value, key) {
    $scope.images.push({
        id: i,
        src: ("data:image/jpeg;base64," + value)
    });
    i = i + 1;
});

// Get the parameter i.e. image that is clicked
$scope.seeOne = function (image) {
    console.log(image.id);
};

查看/模板:

<div id="galscrolldiv" ng-repeat="image in images">
    <div ng-click="seeOne(image)">
    <!--                  ^^^^^-->
        <img ng-src="{{images[$index].src}}" width="100%" />
    </div>
</div>

您還可以從模板發送$ index屬性

<div id="galscrolldiv" ng-repeat="image in images">
    <div ng-click="seeOne($index)">
    <!--                  ^^^^^-->
        <img ng-src="{{images[$index].src}}" width="100%" />
    </div>
</div>

然后在控制器內部

$scope.seeOne = function (index) {
    console.log($scope.images[index].id);
};

但這可能會導致使用過濾器時出現問題。 所以在使用過濾器的情況下避免$ index。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM