Tuesday, 6 August 2013

Do I need separate controllers from component-like directives in angularjs

Do I need separate controllers from component-like directives in angularjs

I am writing custom element directives which are used to encapsulate HTML
GUI components. I am adding custom methods (that handles ng-click events
etc) in my linking function such as:
app.directive('addresseseditor', function () {
return {
restrict: "E",
scope: {
addresses: "="
}, // isolated scope
templateUrl: "addresseseditor.html",
link: function(scope, element, attrs) {
scope.addAddress= function() {
scope.addresses.push({ "postCode": "1999" });
}
scope.removeAddress = function (index) {
scope.addresses.splice(index, 1);
}
}
}
});
Is the link function correct place to define the methods or is it better
to create a separate controller object, use ng-controller and deine
methods there?

No comments:

Post a Comment