0

I'm trying to save an array element in another variable using ng-click in ng-repeat, but when I click the element, it doesn't work.

This is my Code:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="myCtrl">
  <div ng-repeat="img in images">
    <a ng-click="showing = img">{{img.descricao}}</a>
  </div>

  --------------------
  Clicked element description is: {{showing.descricao}}
</body>
</html>

This is my javascript:

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
      $scope.images = [
        {
            "id": 1,
            "image": "http://placehold.it/300x300",
            "ordem": 1,
            "descricao": "Descricao 1"
        },
        {
            "id": 2,
            "image": "http://placehold.it/300x300",
            "ordem": 2,
            "descricao": "Descricao 2"
        }
    ];

  $scope.showing = {};
});

There is my complete code: http://jsbin.com/fuyuci/1/edit

Where I was wrong?

Lai32290
  • 8,062
  • 19
  • 65
  • 99

1 Answers1

3

I think that you're using not correctly when changing value of showing in ng-repeat because ng-repeat create own scope. In stead of that, you should bind an function to set value for $scope.showing. for example:


    $scope.changeShowing = function(img) {
        `$scope.showing = img;`
    };

ng-click ="changeShowing(img)"'

NguaCon
  • 124
  • 9