0

I have a code that looks like this:

<div id="ng-div" ng-app="app">
    <div ng-controller="dataController">
        //... stuff that populates $scope.data
        <button id="submit1" ng-click="processData()">Quote</button>
    </div>
</div>

<script>
    var app = angular.module('app', []);
    app.controller('dataController', ['$scope', '$http', function($scope, $http) {

        $scope.data = [];

        $scope.processData = function(jobs) {
            console.log($scope.data);
        };
    }]);

</script>


<button id="submit2">Quote (Outside scope)</button>

Due to legacy/design limitations, I need to have the Quote button to be somewhere else (which is outside the ng-app div/scope), for example the "submit2" button.

How can I trigger the processData() function using vanilla/jquery?

1 Answer 1

1

try this

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl" id="myCtrl">
</div>
<button onclick="myFunction()">Click me</button>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.test = function(){
    	alert('Hii from controller');
    }
});

function myFunction() {
      angular.element(document.getElementById('myCtrl')).scope().test();
}
</script>

</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is, I cannot do that, as the ng-app is part of an SPA frame, but I need to reuse a global Jquery button generator script to keep them consistent across the site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.