javascript - How do I get an instance of a controller that was created by AngularJS? -
i have app based on angular initialize this:
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) { // want call indexpagecontroller.onswiped(event) here! }, istopcard); }; }) .directive('homedirective', function () { return function (scope, element, attrs) { cards.initpanel(element, function (event) { // want call indexpagecontroller.onbuttonpressed(event) here! }); }; }); angularapp.factory('ajaxservice', myapp.services.ajaxservice); angularapp.controller('indexpagecontroller', ['$scope', '$http', '$sce', 'ajaxservice', myapp.pages.indexpagecontroller]); }());
my controller looks this:
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); }; };
in 2 directives homeiterationdirective
, homedirective
have 2 callbacks cards.initsingleswipe
, cards.initpanel
. within these callbacks want call public methods of controller don't have instance available angular created indexpagecontroller
. how can achieve this?
use (inject) service (and not controller) if want "to call public method" place, possibly controller.
angularapp.controller('mycontroller', function ($scope, indexpageservice) { indexpageservice.blah(); }));
controller intended receive , modify $scope (adding methods, variables..etc). $scope can used inside template (html) use controller itself.
Comments
Post a Comment