I am encountering an issue with my simple controller while fetching and displaying messages using $http.get in HTML using ng-repeat. The problem arises when trying to access the messages from a modal window, as it always prints the first message only.
index.html
<div ng-controller="MessageController as ctrl">
<ul class="list-group" ng-repeat="message in ctrl.messages">
<li class="list-group-item">
{{message.title}}
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">
Edit
</button>
<div class="modal fade" id="myModal" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
{{message.title}}
<label>Title:</label><input ng-model="message.title"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
app.js
var app = angular.module('plunker', []);
var prefix = 'http://jsonplaceholder.typicode.com';
app.controller('MessageController', ['$http', function ($http) {
var $this = this;
$http.get(prefix + '/posts').success(function (response) {
$this.messages = response;
return response;
});
}]);
Here's the plucker http://plnkr.co/edit/ZKyZJV9aYD5AU6kmsMTC?p=preview