Larsi
Larsi

Reputation: 4774

Trigger angular from outside of angular-app

Ho to trigger an event inside ngApp from the outside world?

This is my current code:

    // Inside old code
    console.log("Start Triggering angular");
    $('#issueSelectedPid').val(pid);
    $('#issueSelectedPid').trigger('input');
    console.log("Triggering angular");


    // inside ngApp
    <input id="issueSelectedPid" ng-model="vm.selectedPid" ng-change="vm.doMyStuffTriggerdFromTheOutsideWorld()" />

Are there better ways of doing this?

Thanks for any help

Larsi

Upvotes: 1

Views: 787

Answers (2)

Pavi
Pavi

Reputation: 767

You can access the scope using the below code :

var appElement = document.querySelector('[ng-app=appName]');
var appScope = angular.element(appElement).scope();
var controllerScope = appScope.$$childHead;

controllerScope.functionName(controllerScope.vm.doMyStuffTriggerdFromTheOutsideWorld() in your case)

Upvotes: 3

Narek Mamikonyan
Narek Mamikonyan

Reputation: 4611

you need to get your html element by yourDivId which is defining your input scope, then call $apply with something or without

var scope = angular.element($("#yourDivId")).scope();
    scope.$apply(function(){
        scope.something= 'something';
    })

Upvotes: 0

Related Questions