【发布时间】:2018-03-29 18:50:36
【问题描述】:
在尝试从自定义指令内部访问 $modelValue 和 $viewValue 时,我得到了 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>
我得到这个输出:
Dog's name: Hero
View Name: null
Model Value: null
Tag name: Doggggyyy
【问题讨论】:
标签: angularjs angularjs-directive