get angularJS controller in javascript -
i have controller defined this:
var servicesinstance = angular.module('myapp'); servicesinstance.controller('servicesviewcontroller', function() { console.log("servicesviewcontrollerconstructor."); this.initialize = function() { console.log("servicesviewcontroller initialize."); }; });
and instance of dynamically in code this:
var instance = $injector.get('servicesviewcontroller');
note: please ignore hardcoded strings. post, hardcoded strings. however, in reality, controller name come configuration data (eg: json object).
the instance variable null. $injector.has('servicesviewcontroller') returns false.
why case? correct way controller instance?
thank matt
edit: throwing in more info kept out because trying keep question simple. using requirejs load javascript code creates controller. eg:
define([], function(servicesviewcontrollerprovider) { 'use strict'; var servicesinstance = angular.module('myapp'); servicesinstance.controller('servicesviewcontroller', function() { console.log("servicesviewcontroller constructor"); this.initialize = function() { console.log("servicesviewcontroller initialize."); }; }); var servicesviewcontroller = new object(); servicesviewcontroller.$inject=['$scope']; servicesviewcontroller.view = "/test/servicesview.html"; servicesviewcontroller.requiresui = true; servicesviewcontroller.controllername = "servicesviewcontroller"; return servicesviewcontroller; });
thanks matt
this 1 way know instance of controller
var instance = $controller('servicesviewcontroller');
parsing scope usually
//var instance = $controller('servicesviewcontroller',{$scope: $scope}); //call in service servicesinstance.service('myservice',function ($controller, $rootscope) { var scope = $rootscope.$new(true); //here have servicesviewcontroller scope var instance = $controller('servicesviewcontroller',{scope: $scope}); });
Comments
Post a Comment