I've implemented a feature where double-clicking the name displays an input field, but I'm facing an issue with switching back after a successful put-request. Despite setting $scope.edit to false, it doesn't seem to work as expected. This is my first attempt at working with Angular today.
<ul>
<li ng-repeat="customer in customers">
<label>
<input type="checkbox" ng-model="customer.selected"/>
</label>
<span ng-hide="edit" ng-dblclick="edit = true;">{{ customer.name }}</span>
<label ng-show="edit">
<input type="text" ng-model="customer.name" ng-keyup="editCustomer($index, customer, $event)"/>
</label>,
<small ng-bind="customer.created_at"></small>
<button ng-click="deleteCustomer($index, customer)">-</button>
</li>
</ul>
Controller:
$scope.editCustomer = function (index, customer, event) {
// enter
if (event.keyCode === 13) {
$http.put('customers/' + customer.id, customer).success(function () {
// update customer.name binding
$scope.edit = false;
})
}
// esc, do nothing and revert
else if (event.keyCode === 27) {
$scope.edit = false;
}
};
Is there a more efficient solution for handling this scenario?