4
這是我在嘗試使控制器更加可重用時發現的。AngularJS:使用調用的控制器繼承(或應用)
app.factory('BaseController', function(myService) {
return function(variable) {
this.variable = variable;
this.method = function() {
myService.doSomething(variable);
};
};
})
.controller("ChildController1", function($scope, BaseController) {
BaseController.call($scope, "variable1");
})
.controller("ChildController2", function($scope, BaseController) {
BaseController.call($scope, "variable2");
});
現在我可以做我的HTML這樣的事情(例如NG-控制器= 「ChildController1」 中):NG-點擊= 「方法()」
只簡單的工作。但我不知道它是如何工作的(它是什麼樣的模式?),這樣做會是一個好習慣嗎?