Merlin
Merlin

Reputation: 4917

Store first element of ng-repeat directive

Im new to Angular..

I have a ng-repeat directive and I would like to store one of the answer attribute in a "global" variable.

I would like someting like this

var buffer = answer.discussionId;
<div ng-repeat="answer in answers">
    if(buffer != answer.discussion){
        //do something
        buffer = answer.discussionId;
    }    
</div>

Thank you

Upvotes: 1

Views: 166

Answers (3)

Dalorzo
Dalorzo

Reputation: 20014

If I understood your problem correctly this will be the "angular way" to do this:

You need to create a watch to find when the value you spec has changed.

app.controller('myCtrl',['$scope',function($scope){
    "use strict";
    $scope.buffer ="";
    $scope.answers = [{ /*some object we do not know*/ }];

          $scope.$watch("answers",function(newValue, oldValue, scope){
                  var result =newValue.filter(function(answer){
                       return ($scope.buffer !== answer.discussion);
                  });
                  if (result.length > 0){
                      $scope.buffer = result[0].discussionId;
                  }
          },true);
}]);

For more information watch click here

Upvotes: 1

xspydr
xspydr

Reputation: 3050

With angular the idea of doing conditional logic within your view is discouraged. You should really be doing any storing of data via your controller/model or through the use of directives.

http://docs.angularjs.org/guide/directive

Upvotes: 1

michael
michael

Reputation: 16341

I am sure there will be a hack to do this. But you should never do this in angular. Do it in your controller!

Upvotes: 2

Related Questions