【发布时间】:2015-05-13 08:33:18
【问题描述】:
试图找到构建 Angular 应用程序的“最佳”方式,我发现了几篇最佳实践文章。有了这个输入,我做到了:
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;
}
});
}])
HTML 模板就是这样的:
<div>
<p>{{notificationdata.username}}</p>
<p>{{notificationdata.sid}}</p>
</div>
所以我的问题是,数据更改是否应该被视为 DOM 操作?在控制器中执行此操作的当前版本对我来说似乎更实用(例如设置默认值)。此外,如果我向其中添加更多功能,“指令链接”块将增长并包含比定义更多的功能。我猜应该在指令中根据范围数据更改颜色或隐藏元素等操作。
社区是什么意思?你同意我的假设吗?
谢谢, 雷纳
【问题讨论】:
标签: javascript angularjs angularjs-directive