AngularJS で Controller の継承をしたい
したいなっておもって、Google で検索したところ、stackoverflow で Can an AngularJS controller inherit from another contoller in the same module? っていうスレがあって、これで解決したっぽいのでメモ。
var app = angular.module('angularjs-starter', []);
app.controller('ParentCtrl ', function($scope) {
// I'm the sibling, but want to act as parent
});
app.controller('ChildCtrl', function($scope, $controller) {
$controller('ParentCtrl', {$scope: $scope}); //This works
});
という風にするだけ。簡単ですね。
なんでコントローラを継承したいのかというと、フォームのコントローラをかきたいときに、共通の処理を Base controller にかいておいて、create とか update とか用のフォームを継承してつくりたい的な。
Published: 2014-11-28(Fri) 08:09