I want to make a directive that behaves like a container by transcluding its contents. My html looks like this
<test>
<input type="text" ng-model="name" />
<button ng-click="alertName()">Alert</button>
</test>
Controller and directive are like this
angular.module('app', [])
.controller('TestCtrl', function($scope) {
$scope.name = 'Eric Cartman';
$scope.alertName = function() {
alert($scope.name);
};
})
.directive('test', function() {
return {
restrict: 'E',
template: '<div ng-transclude></div>',
transclude: true,
replace: true,
link: function($scope, elem, attr, ctrl) {
}
};
});
When the page loads I see 'Eric Cartman' in the textbox and when I click 'Alert' button I see 'Eric Cartman' in the dialog box. Till here everything is fine.
The problem is when I change the name in the textbox and hit the 'Alert' button it still alerts 'Eric Cartman'. Where am I going wrong?