I want to use my directive multiple times. Each time I click my button I want the template to be updated accordingly. Intially $scope.item contains item.id = 1. Hence it displays as
Next when I click on the button I change the item.id to 4. Now the result is as follows.
But as you see, the initial item1 displayed has been changed to item4.
I want to first display item1 and on click of button I want to display item4 without changing the initial value displayed. How can I achieve this?
I have the following directive
var app = angular.module('myApp', []);
app.directive('myDirective', function () {
return {
restrict: 'AE',
scope:true,
template: '<div>{{ item.id }}</div>'
}
});
My controller
function MyCtrl($scope,$compile) {
$scope.item = {id: 'item1'};
$scope.hello = function() {
$scope.item = {id: 'item4'};
var dialogTextHTML ="<my-directive item ='item'></my-directive>"
var compiledDialogData = $compile(dialogTextHTML)($scope);
document.getElementById('mycontainer').appendChild(compiledDialogData[0]);
}
}
My html
<div ng-controller="MyCtrl">
<button ng-click="hello()">
Click here
</button>
<div id='container'>
<my-directive item='item'></my-directive>
</div>
</div>

