How to spy on method in the scope that is not binded - angularjs

I'm trying to test that the method "removePlayer" in the following component is called when removing a player with : spyOn(scope, 'removePlayer').and.callThrough(); and expect(scope.removePlayer).toHaveBeenCalled(); but got the following error : "Error: : removePlayer() method does not exist
Usage: spyOn(, )" how can I achieve that please?
.component("playerList", {
templateUrl: "src/js/player/player.html",
bindings: {
list: '<'
},
controller: function() {
this.removePlayer = function(index) {
console.log('Remove player with index : ', index);
if (index > -1) {
this.list.splice(index, 1);
}
};
}
});
With the following test:
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
scope.list = angular.copy(playersList);
element = angular.element('<player-list list="list"></player-list>');
element = $compile(element)(scope);
scope.$apply();
}));
it('should render the text', function() {
// spyOn(scope, 'removePlayer').and.callThrough();
var title = element.find('h1');
var deleteBtn = element[0].querySelector('button');
expect(title.text()).toBe('This table contains ' + playersList.length + ' player.');
deleteBtn.click();
// expect(scope.removePlayer).toHaveBeenCalled();
expect(title.text()).toBe('This table contains ' + (playersList.length - 1) + ' player.');
});
here is the template :
<div class="container">
<h1 class- "player-table-title">This table contains {{ $ctrl.list.length }} players[![enter image description here][1]][1].</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Team</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="player in $ctrl.list">
<td>{{ player.name }}</td>
<td>{{ player.position }}</td>
<td>{{ player.team }}</td>
<td>
<button type="button" class="btn btn-danger btn-sm" ng-click="$ctrl.removePlayer($index)">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
This is how the app look like in browser:

This method is controller method, not a scope. By the way, components don't have a scope.
Try to get controller of your component like this (don't forget to inject $componentController)
controller = $componentController('playerList', {$scope: scope});
And then replace scope to controller in spyOn function
spyOn(controller, 'removePlayer').and.callThrough();
....
expect(controller.removePlayer).toHaveBeenCalled();

I was able to resolve that by doing : element.controller('playerList');
it('should render the text', function() {
var component = element.controller('playerList');
spyOn(component, 'removePlayer').and.callThrough();
var title = element.find('h1');
var deleteBtn = element[0].querySelector('button');
expect(title.text()).toBe('Ce tableau contient ' + playersList.length + ' joueurs.');
deleteBtn.click();
expect(title.text()).toBe('Ce tableau contient ' + (playersList.length - 1) + ' joueurs.');
expect(component.removePlayer).toHaveBeenCalled();
});

Related

AngularJS - DOM not updated in Chrome and Mozilla

I'm using angularjs and angular-ui-bootstrap to make a table and page some data, which I get from an API.
I've made sure my service receives the correct data and that the requests are built properly. My pageChange function is properly triggered and my first page gets loaded successfully.
So I have the following controller setup:
(function () {
'use strict';
initContacts.$inject = ['$scope', '$http'];
angular.module('app')
.config(function ($locationProvider) {
$locationProvider.html5Mode(true);
});
angular.module('app', ['ui.bootstrap']).controller('contactSearchController', initContacts);
function initContacts($scope, $http) {
$scope.contacts = [];
$scope.totalItems = 0;
$scope.pages = 0;
$scope.currentPage = 0;
$scope.maxPageLinksShown = 5;
if (window.location.hash !== '') {
$scope.currentPage = window.location.hash.replace('#', '');
}
$http.get("/api/ContactApi/GetPage?pageIndex=" + ($scope.currentPage - 1)).success(function (data) {
$scope.contacts = data.Contacts;
$scope.totalItems = data.Count;
$scope.PageSize = data.Contacts.length;
$scope.pages = Math.ceil((data.Count / $scope.PageSize));
});
$scope.pageChanged = function () {
$http.get("/api/ContactApi/GetPage?pageIndex=" + ($scope.currentPage - 1))
.success(function (data) {
$scope.contacts = data.Contacts;
});
};
}
}
})();
And in my view I have:
<div ng-app="app" ng-controller="contactSearchController">
<table class="table table-striped table-hover contact-search-table">
<thead>
<tr>
<td class="contact-title">
Titel
</td>
<td class="department">
Afdeling
</td>
<td class="name">
Navn
</td>
<td class="work-phone">
Telefon
</td>
<td class="mobile">
Mobile
</td>
<td class="email">
Email
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, value) in contacts">
<td class="contact-title">{{value.Title}}</td>
<td class="department">{{value.Department}}</td>
<td class="name">{{value.FullName}}</td>
<td class="work-phone">{{value.WorkPhone}}</td>
<td class="mobile">{{value.WorkMobile}}</td>
<td class="email">{{value.Email}}</td>
</tr>
</tbody>
</table>
<div class="col-xs-12 pager-container">
<ul uib-pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" max-size="maxPageLinksShown" class="pagination-sm" boundary-links="true" num-pages="pages"></ul>
</div>
Now this works to some extent.
My problem
When I click the page links, the pageChanged() function is called, I get the data from my api and it's all correct, the list in the scope appears to be updated fine, but the table in my view doesn't change.
This solution works fine ONLY in IE ! (who would have thought huh...)
No exceptions get thrown.
I swear this was working yesterday.
Any help is much appreciated!
EDIT
What I've tried:
-Putting the assignment of the contacts in an $apply like so:
$scope.pageChanged = function () {
$http.get("/api/ContactApi/GetPage?pageIndex=" + ($scope.currentPage - 1))
.success(function (data) {
$scope.$apply(function () { // apply
$scope.contacts = data.Contacts;
});
});
};
I got a "$digest already in progress" error from this.
tried to wrap the apply in a timeout like so:
$timeout(function(){ //any code in here will automatically have an apply run afterwards });
Got rid of the error but the DOM still won't update.
I had such a problem and I solved it using $scope.$apply:
$scope.pageChanged = function () {
$http.get("/api/ContactApi/GetPage?pageIndex=" + ($scope.currentPage - 1))
.success(function (data) {
$scope.$apply(function () { // apply
$scope.contacts = data.Contacts;
});
});
};
You should use $scope.$applyAsync instead. It will be applied to the next digest cycle.
If you need to target all the $scopes of your AngularJS application, use $rootScope.$applyAsync instead.
Official doc
Okay so what worked in this situation was changing the way I'm visualizing the ng-repeat like so:
<tr ng-repeat="contact in getContacts()">
<td class="contact-title">{{contact.Title}}</td>
<td class="department">{{contact.Department}}</td>
<td class="name">{{contact.FullName}}</td>
<td class="work-phone">{{contact.WorkPhone}}</td>
<td class="mobile">{{contact.WorkMobile}}</td>
<td class="email">{{contact.Email}}</td>
</tr>
And heres the getContacts() function:
$scope.getContacts = function () {
var data = $scope.contacts;
if (data instanceof Array) {
return data;
} else {
return [data];
}
}
I don't really know why that works, but it does.

AngularJS - Not update table-pagination after http-get success's event

i have a problem. I'm using AngularJs with WebService-Rest, and can't update some table after the call HTTP-GET to WebService. I did tested everything but i can't get it.
Next, i attach the code. Thanks!
HTML:
...
<div class="row" ng-app="SIGA" ng-controller="CreateTable">
<div class="container-fluid">
<table class="table table-striped">
<tbody>
<tr>
<td>Buscar</td>
<td><input type="text" ng-model="search.nombre" /></td>
</tr>
<tr ng-repeat="e in estaciones | filter:paginate| filter:search" ng-class-odd="'odd'">
<td>
<button class="btn">
Detalle
</button>
</td>
<td>{{e.nombre}}</td>
</tr>
</tbody>
</table>
<pagination total-items="totalItems" ng-model="currentPage"
max-size="5" boundary-links="true"
items-per-page="numPerPage" class="pagination-sm">
</pagination>
</div>
</div>
...
JS: ...
app.controller('RestEstacion', function ($rootScope, $http) {
$http.get('http://localhost:8080/sigarest/webresources/entity.estaciones').
success(function(data) {
$rootScope.estaciones = data; UpdateTable($rootScope);
}).
error(function(status) {
alert('error:'+status);
});
});
app.controller('CreateTable', function ($scope,$rootScope) {
$rootScope.predicate = 'nombre';
$rootScope.reverse = true;
$rootScope.currentPage = 1;
$rootScope.order = function (predicate) {
$rootScope.reverse = ($rootScope.predicate === predicate) ? !$rootScope.reverse : false;
$rootScope.predicate = predicate;
};
$rootScope.estaciones = [];
$rootScope.totalItems = $rootScope.estaciones.length;
$rootScope.numPerPage = 5;
$rootScope.paginate = function (value) {
var begin, end, index;
begin = ($rootScope.currentPage - 1) * $rootScope.numPerPage;
end = begin + $rootScope.numPerPage;
index = $rootScope.estaciones.indexOf(value);
return (begin <= index && index < end);
};
});
JS (Update Function):
function UpdateTable($rootScope){
$rootScope.totalItems = $rootScope.estaciones.length;}
** Original Answer (what comments refer to) **
I think you are assigning the get response object rather than the data inside it. Try this:
success(function(response) {
$rootScope.estaciones = response.data;
UpdateTable($rootScope);
}).
** EDIT **
Now that we have established that you are returning data from the API, the real issue appears to be the double controller using $rootScope as a bridge, which can work but is a bit of an anti-pattern in Angular.
The first controller in your app is trying to act like a service, and so really needs to be converted into one. Here is some SAMPLE PSUEDO CODE to give the idea. I do not fully understand your code, like the pagination directive. There should be a click handler in the pagination directive that would call the service method changePagination and pass in the new page number. There should be no need for $rootScope anywhere in this.
JS
app.service('RestEstacionService', function ($http) {
var RestEstacionService = this;
this.apiData = null;
this.tableData = null;
this.currentPage = 1;
this.numPerPage = 5;
this.url = 'http://localhost:8080/sigarest/webresources/entity.estaciones';
this.getData = function (url) {
return $http.get(url).then(function(response) {
RestEstacionService.apiData = response.data;
// do success stuff here
// figure out which page the view should display
// assign a portion of the api data to the tableData variable
})
};
this.changePagination = function (newPage) {
// do your your pagination work here
};
});
app.controller('RestEstacionController', ['$scope', 'RestEstacionService', function ($scope, RestEstacionService) {
$scope.service = RestEstacionService;
RestEstacionService.getData(RestEstacionService.url);
}]);
HTML
<div class="row" ng-app="SIGA" ng-controller="RestEstacionController">
<div class="container-fluid">
<table class="table table-striped">
<tbody>
<tr>
<td>Buscar</td>
<td><input type="text" ng-model="search.nombre" /></td>
</tr>
<tr ng-repeat="row in services.tableData | filter:paginate| filter:search" ng-class-odd="'odd'">
<td>
<button class="btn">
Detalle
</button>
</td>
<td>{{row.nombre}}</td>
</tr>
</tbody>
</table>
<pagination total-items="services.apiData.length" ng-model="services.currentPage"
max-size="5" boundary-links="true"
items-per-page="services.numPerPage" class="pagination-sm">
</pagination>
</div>

How to get data from smart-table plugin?

I am trying to adapt the example of how to use a custom plugin with smart-table. See the "Create your own plugin" section of http://lorenzofox3.github.io/smart-table-website/
Here is a screen shot of my web app so far:
The problem I have is that I do not know how to get the items select from the smart-table. The selected items on the smart-table are "Beer" and "Mountain Biking". Here's my app.js file:
var app = angular.module("app", ['smart-table']);
app.directive('csSelect', function () {
return {
require: '^stTable',
template: '<input type="checkbox"/>',
scope: {
row: '=csSelect'
},
link: function (scope, element, attr, ctrl) {
element.bind('change', function (evt) {
scope.$apply(function () {
ctrl.select(scope.row, 'multiple');
});
});
scope.$watch('row.isSelected', function (newValue, oldValue) {
if (newValue === true) {
element.parent().addClass('st-selected');
} else {
element.parent().removeClass('st-selected');
}
});
}
};
});
Here is my controller:
app.controller("vendorCtrl", function($scope,$http) {
$scope.vendor_to_look_for = "";
$scope.newvendor = "";
$scope.newlogo = "";
$scope.vendors_types = [];
$scope.itemsByPage=5;
$http.get("http://192.168.1.115:8080/vendor").then(function(response) {
$scope.all_vendors = response.data;
});
$http.get("http://192.168.1.115:8080/type").then(function(response) {
$scope.all_types = response.data;
});
$scope.submit_vendor = function() {
// alert("add new vendor [" + $scope.newvendor + "]" );
var data = $.param({ vendor_name: $scope.newvendor, vendor_logo: $scope.newlogo, vendors_types: $scope.vendors_types });
$http.post("http://192.168.1.115:8080/vendor/add",
// [{'vendor': $scope.newvendor}],
data,
{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
console.log(response);
console.log(response.data);
});
$http.get("http://192.168.1.115:8080/vendor").then(function(response) {
console.log(response);
console.log(response.data);
$scope.all_vendors = response.data;
});
};
});
Update: Here's the pertinent HTML:
<form ng-submit="submit_vendor()">
<table>
<tr><td><label>Vendor name</label></td><td><input type="text" ng-model="newvendor" placeholder="Name"></td></tr>
<tr><td><label>Logourl</label></td><td><input type="text" ng-model="newlogo" placeholder="Url"></td></tr>
</table>
<table st-table="displayedCollection" st-safe-src="all_types" class="table table-striped">
<thead>
<tr>
<th st-sort="type">Select</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in displayedCollection">
<td cs-select="x"></td>
<td>{{x.type}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" class="text-center">
<div st-pagination="" st-items-by-page="itemsByPage" st-displayed-pages="7"></div>
</td>
</tr>
</tfoot>
</table>
<button type="submit" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-flash"></span> Add </button>
</form>
When I click the add button my submit_vendor() function executes but my the vendor_types I pass in my form data is empty. How do I get the items selected in my table into my form data?
Thanks!
I found this https://github.com/vitalets/checklist-model
This makes things a lot easier. I do not need to declare my csSelect directive :)

How do I get $index from an ng-repeat table to a modal controller?

I have a table of customer records created with angularjs bootstrap-ui and uses ng-repeat.
At the end of each line in the table is a button to get more information about the customer.
When the button is clicked a modal form pops with the information.
my problem is whichever button I press I get the same customer number
The problem is I need to get the value of $index to the following bit of code:
$scope.selected = {
customer: $scope.customers[0]
};
The value of $index needs to replace the 0 value above
What I have done so far can be seen on plunker click here
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
< div class = "modal-header" > < h3 > I am a modal! < /h3>
</div > < div class = "modal-body" > < form class = "form-horizontal"
role = "form" > < div class = "form-group" > < label
for = "customerNumber"
class = "col-lg-2 control-label" > Email Address: < /label>
<div class="col-lg-10">
<input type="text" class="form-control" id="customerNumber"
ng-model="selected.customer.customerNumber"
ng-value="selected.customer.customerNumber">
</div > < /div>
</form > < /div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button > < button class = "btn btn-warning"
ng - click = "cancel()" > Cancel < /button>
</div >
</script>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Customer number</th>
<th>Customer name</th>
<th>Customer last name</th>
<th>Customer first name</th>
<th>phone</th>
</tr>
</thead>
<tbody ng-repeat="customer in customers">
<tr>
<td>{{ customer.customerNumber }}</td>
<td>{{ customer.customerName }}</td>
<td>{{ customer.contactLastName }}</td>
<td>{{ customer.contactFirstName }}</td>
<td>{{ customer.phone }}</td>
<td>
<button class="btn btn-default" ng-click="open()">
Customer details
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
'use strict';
angular.module('myApp', ['ui.bootstrap'])
.controller('ModalDemoCtrl', function ($scope, $modal, $log) {
$scope.customers = [
{
"customerNumber": 103,
"customerName": "Atelier graphique",
"contactLastName": "Schmitt",
"contactFirstName": "Carine ",
"phone": "40.32.2555"
},
{
"customerNumber": 112,
"customerName": "Signal Gift Stores",
"contactLastName": "King",
"contactFirstName": "Jean",
"phone": "7025551838"
},
{
"customerNumber": 114,
"customerName": "Australian Collectors, Co",
"contactLastName": "Ferguson",
"contactFirstName": "Peter",
"phone": "03 9520 4555"
}
];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
customers: function () {
return $scope.customers
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function ($scope, $modalInstance, customers) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}
ng-repeat directive has a variable
$index
You can pass this variable in the click function like this
<button class="btn btn-default" ng-click="open($index)">
Customer details
</button>
You need to accept this index as a parameter in your method, so just add the parameter
$scope.open = function (index) {
.... your method body
}
Pass the customer object to your function first:
ng-click="ok(customer)"
Then find the index based on that object in your function:
$scope.ok = function (customer) {
var index = $scope.customers.indexOf(customer);
$scope.selected.customer = $scope.customers[index];
$modalInstance.close($scope.selected.customer);
};
Don't do it like this - instead work with actual customer objects. Correct me if I am wrong but it looks like you have some kind of list of customers that when you click on them opens a modal with more details. Try something like this:
In the customer table:
<tbody ng-repeat="customer in customers">
<tr>
<td>{{ customer.customerNumber }}</td>
<td>{{ customer.customerName }}</td>
<td>{{ customer.contactLastName }}</td>
<td>{{ customer.contactFirstName }}</td>
<td>{{ customer.phone }}</td>
<td>
<button class="btn btn-default" ng-click="open(customer)">
Customer details
</button>
</td>
</tr>
</tbody>
In the controller:
$scope.open = function(customer){
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
customer: function () {
return customer;
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
And in the modal controller:
var ModalInstanceCtrl = function ($scope, $modalInstance, customer) {
$scope.customer = customer;
$scope.ok = function () {
$modalInstance.close($scope.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}
And finally, in the modal view:
<script type="text/ng-template" id="myModalContent.html">
< div class = "modal-header" > < h3 > I am a modal! < /h3>
</div > < div class = "modal-body" > < form class = "form-horizontal"
role = "form" > < div class = "form-group" > < label
for = "customerNumber"
class = "col-lg-2 control-label" > Email Address: < /label>
<div class="col-lg-10">
<input type="text" class="form-control" id="customerNumber"
ng-model="customer.customerNumber"
ng-value="customer.customerNumber">
</div > < /div>
</form > < /div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button > < button class = "btn btn-warning"
ng - click = "cancel()" > Cancel < /button>
</div >
</script>
Thank you for your suggestions Rob and Fourth.
I used a combination of both your suggestions
You can see the result on plunker by clicking here
The changes are shown below
<td>
<button class="btn btn-default"
ng-click="open(customer)"> <!-- Customer object now goes to controller-->
Customer details
</button>
</td>
Inside the controller the customer object is in scope of the open function only.
I had to get index so I could use it with the customers object
resolve: {
customerIndex: function () {
return $scope.customers.indexOf(customer)
},
customers: function () {
return $scope.customers
}
}
Both customers and customerIndex are used in the ModalInstanceCtrl
var ModalInstanceCtrl = function ($scope, $modalInstance, customers, customerIndex) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[customerIndex]
};
it should be even simpler if you directly use $index. Hence, you will not require to lookup for the index using the customer object in your controller. Please have a look at the following it could also be tested on, plunker
index.html
<td>
<button class="btn btn-default" ng-click="open($index)">
Customer details
</button>
</td>
example.js
// code above this controller method wasn't modified
$scope.open = function (index) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
index: function() {
return index;
},
customers: function () {
return $scope.customers
}
}
});
modalInstance.result.then(function (selectedCustomer) {
$scope.selected = selectedCustomer;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
var ModalInstanceCtrl = function ($scope, $modalInstance, customers, index) {
$scope.customers = customers;
$scope.selected = {
customer: $scope.customers[index]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.customer);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
}
}

Isolate scope variable is undefined

For some reason I am not able to access my isolate scope variable "index" in my link function. I have tried both # and = to see if it made any difference and this had no effect, I also tried using a word other than index (in case it was already in use by angular or something) with the same result. Error msg: ReferenceError: index is not defined.
directive:
var jobernizeDirectives = angular.module('jobernizeDirectives', []);
jobernizeDirectives.directive('viewLargeContent', function() {
return {
scope: {
index: '=index'
},
require: 'ngModel',
link: function (scope, el, attrs, ngModel) {
console.log(index);
function openEditor() {
console.log(index);
var main_container = angular.element('.main_container');
var view_frame = angular.element('.view-frame');
console.log(ngModel);
main_container.append('<div class="dimmer"></div>');
main_container.append(
'<div class="large-content' + index + '">'
+ '<textarea>' + ngModel.$viewValue + '</textarea>'
+ '<button>Submit Changes</button>'
+ '</div>'
);
var dimmer = angular.element('.dimmer');
var content_container = angular.element('.large-content' + index);
// content_container.css({ marginTop: (content_container.height()/-2), width: view_frame.width(), marginLeft: (view_frame.width()/-2) })
content_container.css({ height: (0.8*main_container.height()), width: view_frame.width(), marginLeft: (view_frame.width()/-2) })
content_container.find('button').on('click', function() {
var new_content = content_container.find('textarea').get(0).value;
ngModel.$setViewValue(new_content);
content_container.hide();
dimmer.hide();
});
}
// el.on('click', openEditor);
el.on('click', openEditor);
}
}
});
html:
<div data-ng-controller="resumesController">
<div class="row">
<div class="small-12 columns">
<h2>Your Resumes</h2>
</div>
</div>
<div class="row">
<div class="small-12 columns">
<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Date Added</th>
<th>View/Edit Resume</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="resume in resumes">
<td>{{ resume.name }}</td>
<td>{{ resume.category }}</td>
<td>{{ resume.date_added }}</td>
<td><button index="le" view-large-content data-ng-model="resume.content">View Resume</button></td>
</tr>
</tbody>
<table>
</div>
</div>
</div>
Index is an attribute of you scope object, so you access it like this:
scope.index
Both '#' or '=' will work, the difference is that '#' will interpolate the value and update the isolated scope when it changes and '=' means Angular should keep the attribute and the isolated scope in sync.
If the variables have the same name you can use the syntactic sugar
scope: {
index: '#'
},
Here is a JSBin with your code.

Resources