While trying to access $modelValue
and $viewValue
from inside custom directives, I am getting null
.
var demo = angular.module('demo', []);
demo.directive('dogInfo', function($parse) {
return {
restrict: 'E',
require: 'ngModel',
template: "<div> Dog's name: {{dogname}}<br>View Name: {{viewValue}}<br>Model Value: {{modelValue}}<br> Tag name: {{tagName}}</div>",
link: function (scope, element, attrs, controller) {
scope.viewValue = controller.$viewValue;
scope.modelValue = controller.$modelValue;
scope.tagName = controller.$name;
}
}});
function MyCtrl ($scope) {
$scope.dogname = 'Hero'; // now
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<div ng-app='demo'>
<p ng-controller='MyCtrl'>
<dog-info ng-model="dogname" name="Doggggyyy"/>.
</p>
</div>
I am getting this output :
Dog's name: Hero
View Name: null
Model Value: null
Tag name: Doggggyyy
Here's the fiddle : http://jsfiddle.net/cxdun3y8/3/