I've built a directive has an object passed to it via bindToController, with the intent of editing it. Edit was working great until I needed to cancel an edit. To undo, I needed to create a shadow copy of the original object, edit it, then copy it back or discard based on save or cancel. I've tried to achieve this in the link property of the directive, using scope.watch on the the controller property. The watch fires once, when initialized, the property is undefined because nothing has used it yet, which is expected. It never fires again though, once a real object is put into the property.
Where have I gone wrong? Should I got back to using $scope because I'm having issues getting a reference to the controller? Why is that watch only firing once?
The directive:
angular.module("ISG").directive('isgEditingFundDirective', function () {
var ctrl = null;
var isgEditingFundDirectiveController = function () {
ctrl = this; // Getting a reference to the controller so it can be used in the link function. Is there a better way to do this?
this.cancel = function () {
// Undo the edit
ctrl.fund = null;
};
this.save = function () {
// Save back to the original model
angular.copy(ctrl.shadow, ctrl.fund);
// Set to null because we aren't editing anymore
ctrl.fund = null;
}
}
var isgEditingFundDirectiveLink = function (scope, element, attrs) {
// need link so we can undo an edit
scope.$watch(ctrl.fund, function (orig, updated) {
// Trying to watch the fund property in the controller so I can create a copy for undo later.
// This never fires with a real value
angular.copy(ctrl.fund, ctrl.shadow);
});
}
return {
restrict: "E",
controllerAs: 'editFundCtrl',
templateUrl: "/angular/editfund",
bindToController: {
fund: "=fund"
},
controller: isgEditingFundDirectiveController,
link: isgEditingFundDirectiveLink
};
});
The Template:
Editing fund
Name:
<input ng-model="editFundCtrl.shadow.FundName"/>
<button ng-click="editFundCtrl.cancel()">Cancel</button>
<button ng-click="editFundCtrl.save()">Save</button>
<pre>{{editFundCtrl.fund}}</pre>