I've learned the basics about AngularJS but I've just hit a wall. I want a directive that creates a textbox where I can respond to changes in the value using $observe. I've tried everything I can think of but I guess I'm missing something fundamental. I am getting 'undefined - Fubar!' in the console on page refresh but no further logs when the value changes. Here's what I have at the moment:
HTML
<attr-peek attr1="{{fubar}}"></attr-peek>
Javascript
app.controller("observeController", function ($scope) {
$scope.fubar = "Fubar!";
});
app.directive("attrPeek", function () {
return {
restrict: "E",
replace: true,
template: "<input type='text' value='{{attr1}}' />",
scope: { attr1: "@" },
link: function (scope, element, attrs, controller) {
console.log(attrs);
attrs.$observe("value", function (newValue, oldValue) {
console.log(oldValue + " - " + newValue);
});
}
};
});
Fiddle http://jsfiddle.net/kp226/
If someone could show me how to do this I'd really appreciate it.
attrs.$observe
(annoyingly) only takes the new value as an argument (which is why you're getting"undefined - Fubar!"
logs). – Spiny Norman Mar 13 at 14:21