我有基於角的應用程序,我初始化這樣的:如何獲取由AngularJS創建的控制器實例?
myapp.init = (function() {
'use strict';
var angularApp = angular.module('myapp', [])
.directive('homeIterationDirective', function() {
return function (scope, element, attrs) {
var isTopCard = scope.$last ? true : false;
cards.initSingleSwipe(element.get(0), function (event) {
// I want to call indexPageController.onSwiped(event) here!
}, isTopCard);
};
})
.directive('homeDirective', function() {
return function (scope, element, attrs) {
cards.initPanel(element, function (event) {
// I want to call indexPageController.onButtonPressed(event) here!
});
};
});
angularApp.factory('AjaxService', myapp.services.AjaxService);
angularApp.controller('IndexPageController', ['$scope', '$http', '$sce', 'AjaxService', myapp.pages.IndexPageController]);
}());
我的控制器看起來是這樣的:
myapp.pages.IndexPageController = function ($scope, $http, $sce, MyService) {
'use strict';
var somevalue = {};
this.onSwiped = function (event) {
doSomethingWith(event, somevalue);
};
this.onButtonPressed = function (event) {
doSomethingWith(event, somevalue);
};
};
在2個指令homeIterationDirective
和homeDirective
我有2個回調cards.initSingleSwipe
和cards.initPanel
。在這些回調中,我想調用我的控制器的公共方法,但是我沒有可用的角度從IndexPageController
創建的實例。我怎樣才能做到這一點?
你想去哪裏調用此方法?通常,在控制器內定義的方法可用於相應的作用域。 –
@DarinDimitrov我想從我初始化控制器的位置調用它,所以在'angularApp.controller(...)'的下面' – Timo
只需調用'this.blah()' – Chandermani