クソコード を書いたはいいが、釈然としなかった。 "View independent business logic: Services" とドキュメントに書いてある通りなので、やはり他のやりかたのほうがよい。のでよくよく読んだところ、やはり View にかかわる部分は Directive に集約されるようだ…… しかし使いかたが非常に難しい。
iimsApp.directives.directive('dialog', function ($q) {
return {
restrict: 'E',
transclude : true,
scope : {
Dialog : '=name'
},
controller : function ($scope) {
$scope.Dialog = {
open : function (title) {
$scope.deferred = $q.defer();
$scope.title = title;
$scope.show = true;
return $scope.deferred.promise;
}
};
$scope.close = function () {
$scope.show = false;
};
$scope.ok = function () {
$scope.close();
$scope.deferred.resolve();
};
$scope.cancel = function () {
$scope.close();
$scope.deferred.reject();
};
},
templateUrl : '/static/js/app/dialog.html'
};
}); こんな感じで dialog という directive を定義して、
<div ng-controller="FooCtrl">
<dialog name="FooBarDialog">
{{ message }}
</dialog>
</div> というふうにすると、FooCtrl のスコープから FooBarDialog というプロパティで見えるようになる。つまり
$scope.message = "Hello!";
$scope.FooBarDialog.open('OKK!!!!!').then(function () {
alert('ok');
}, function () {
alert('cancel');
}); みたいなあ
-
トップ
-
tech
-
Angular JS で View を伴う Service 的なことをしたいとき、あるいは Directive に Controller をつけたいとき。