index.html
represents:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="js/angular.js" type="text/javascript"></script>
<script src="js/app.js" type="text/javascript"></script>
</head>
<body>
<div>TODO write content </div>
<input test type="text" ng-model="name" >
<h1>name: {{name}}</h1>
</body>
</html>
app.js
signifies:
var app = angular.module('myApp', []);
app.directive('test', function () {
return {
require: '?ngModel',
link: function ($scope, $element, $attr, controller) {
if (!controller) {
console.log("controller of ngModel not found");
return;
} else {
console.log("controller of ngModel found");
controller.$setViewValue('qwerty');
}
}
};
});
In the above example, in the link
function, I am accessing the controller
of the ngModel
directive by utilizing the require
option specified in the DDO
. I then update the value of name
, which reflects in the
<h1>name: {{name}}</h1>
element within index.html
, but not within the <input test type="text" ng-model="name">
element also inside index.html
. Why does it update in one place but not the other?
Code @ plnkr.co