2

Simply, I have an array of objects, [images], that I display perfectly fine on Ng-repeat. When I click one div, I want to see the 'id' property of the particular image that was clicked on in the template.

Function in controller:

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);
}

My template:

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

2 Answers 2

2

Send the image as parameter from the view/template. Then you can access the image properties of the clicked image.

You've missed the ) of forEach.

Controller:

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);
};

View/Template:

<div id="galscrolldiv" ng-repeat="image in images">
    <div ng-click="seeOne(image)">
    <!--                  ^^^^^-->
        <img ng-src="{{images[$index].src}}" width="100%" />
    </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Of course! I was overthinking it and forgetting this simple fact. Thank you!
1

You can also send the $index property from template

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

Then inside the controller

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

But this may cause issue while using filter. So in case of using filter avoid $index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.