, body-body angular.
html ,
<div ng-app="app" ng-controller="appController">
<table class=table>
<tbody>
<tr ng-repeat="item in items" ng-click="selectItem(item)" ng-style="item.id === selectedItem.id && selectedStyle">
<td>
{{item.id}}
</td>
</tr>
</tbody>
</table>
<div class="item-body" my-dir ng-model="myValue">
{{selectedItem.name}}
</div>
</div>
Remember to download angular -animate.js and add it to your solution. The important part is to add the dependency to 'ngAnimate' in your module and add your custom directive as follows.
Before that, add a class style.
.myClass
{
background-color: red;
transition: all 1s;
-webkit-transition: all 1s ease-in-out;
}
Note that I use the $ watch method to track a variable
var app = angular.module('app', ['ngAnimate']);
app.controller('appController', function ($scope) {
$scope.items = [
{ "id": "id1", "name": "Name 1" },
{ "id": "id2", "name": "Name 2" },
{ "id": "id3", "name": "Name 3" }
];
$scope.selectedStyle = { "background-color": "blue", "color": "white" };
$scope.selectedItem = $scope.items[0];
$scope.selectItem = function (item) {
$scope.selectedItem = item;
$scope.myValue = item.name;
}
}).directive("myDir", function ($animate, $timeout) {
return function (scope, element, attrs) {
scope.$watch('myValue', function (newValue, oldValue) {
if (newValue != oldValue) {
$animate.addClass(element, "myClass").then(function () {
$timeout(function () { $animate.removeClass(element, "myClass"); });
});
}
},true);
}
});
source
share