var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.products = [{"id":1,"qty":10},{"id":2,"qty":20},{"id":3,"qty":30}]
$scope.name = 'World';
$scope.editMode = false;
});
app.directive('editable',function(){
return{
restrict : 'E',
replace : true,
templateUrl: 'editable.html',
link : function(scope, element, attrs){
// find the input elemnt of this directive ...
var input = element.find('input');
// and listen for blur event
input.bind('blur', function(){
// since blur event occured ouside the angular execution context
// we need to call scope.$apply to tell angularjs about the changes
scope.$apply(function(){
// the change is to disable the editMode
scope.editMode = false;
});
});
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link data-require="bootstrap-css" data-semver="3.0.2" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
<style>
body {
padding-top: 60px;
}
@media (max-width: 979px) {
/* Remove any padding from the body */
body {
padding-top: 0;
}
}
</style>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.5/angular.js" data-semver="1.2.5"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<!-- Table with editable qty cell -->
<p>Editable table</p>
<table class="table">
<tr><th>Id</th><th>Qty</th></tr>
<tr ng-repeat="product in products">
<td>{{product.id}}</td>
<td><editable text="product.qty"></editable></td>
</tr>
</table>
<br>
<!-- Not editable table, which reflects changes in the first one -->
<p>Not editable</p>
<table class="table">
<tr><th>Id</th><th>Qty</th></tr>
<tr ng-repeat="product in products">
<td>{{product.id}}</td>
<td>{{product.qty}}</td>
</tr>
</table>
</body>
</html>
/* Put your css in here */
<div>
<span ng-hide="editMode" ng-click="editMode = true">{{product.qty}}</span>
<input class="line-qty" ng-show="editMode" type="text" ng-model="product.qty"/>
</div>