Angular - object index

http://stackoverflow.com/questions/19464979/create-simple-alias-for-long-expression-in-template/19466193#19466193

by markokristian

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-app="myApp">
<div ng-controller="myController">
    
    <ul>
        <li ng-repeat="item in model.list">
            <span ng-click="model.updateIndex(item.id)">
                Show {{ item.name }}
            </span>
        </li>
    </ul>
    
    <div>Now showing: </div>
    <div>{{ model.current().name }}</div>
    
</div>
</div>

JavaScript

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

var Model = function(){
    this.list = [
        {'id':0, 'name':'one'},
        {'id':1, 'name':'two'}
    ];
    this.objectIndex = 0;
    
    this.updateIndex = function(index){
        this.objectIndex = index;
    };
    
    this.current = function(){
        return this.list[this.objectIndex];
    };
};

app.controller("myController",function($scope) {
    $scope.model = new Model();
});