Friday, 8 May 2015

Call Angular from JS

I have another post discussed this topic:

http://baiduhix.blogspot.co.uk/2014/09/update-angularjs-from-js.html

I want to say more about it:

Call a services from js:

http://stackoverflow.com/questions/17319784/call-angularjs-service-from-simple-js-code

angular.module('main.app', []).factory('MyService', ['$http', function ($http) {
    return new function () {

        this.GetName = function () {
            return "MyName";
        };
    };
}]);


angular.injector(['ng', 'main.app']).get("MyService").GetName();
http://jsfiddle.net/wGeNG/

Call controller from js:

angular.element(document.getElementById('control-panel')).scope().$apply(function(scope){
        scope.filters = dictt.filters;
    });
Or:

angular.element(document.getElementById('ngview')).scope().functionxxxxxx();

If you are using :

<div id="ngview" ng-view=""></div> and ng-route to manage your view and controllers. you can get your controller by:

angular.element(document.getElementById('ngview')).scope().functionxxxxxx();





No comments:

Post a comment