Trying to find the "best" way building an Angular App I found several best practice articles. With this input I did this:
angular.module('xApp', [])
//..... some services, factories, controllers, ....
.directive('dirNotification',[ function dirNotification() {
return {
scope: {},
templateUrl: 'xNotification.html',
replace: true,
controller: 'CtrlNotification',
link: function($scope){
// if this is 'DOM manipulation, should be done here ... ?
/*
$scope.$on('session.update',function(event, args) {
if (args == null) {
$scope.notificationdata.username = "";
$scope.notificationdata.sid = "";
} else {
$scope.notificationdata.username = args.username;
$scope.notificationdata.sid = args.accessToken;
}
});
*/
}
};
}])
.controller('CtrlNotification',['$scope' ,function CtrlNotification($scope) {
$scope.notificationdata = {
username: "",
sid: ""
};
// this is not real DOM manipulation, but only view data manipulation?
$scope.$on('session.update',function(event, args) {
if (args == null) {
$scope.notificationdata.username = "";
$scope.notificationdata.sid = "";
} else {
$scope.notificationdata.username = args.username;
$scope.notificationdata.sid = args.accessToken;
}
});
}])
The HTML template is simply this:
<div>
<p>{{notificationdata.username}}</p>
<p>{{notificationdata.sid}}</p>
</div>
So my question is, should data changes to be considered as DOM manipulation? The present version doing this within the controller seems more practical to me (e.g. setting default values). Also if I add more functionality to that, the "directive link" block will grow and contain more functions than definitions. I guess within the directive things like changing colors or hiding elements depending on the scope data should be done there.
What does the community mean? Do you agree with my assumptions?
Thanks, Rainer