2

I need to call an AngularJS function call from javascript-side.

The below is the function I want to call from javascript.

https://github.com/aws-samples/aws-iot-examples/blob/master/mqttSample/js/app.js

AppController.$inject = ['$scope'];
AppController.prototype.createClient = function() {

    var options = {
      clientId : this.clientId,
      endpoint: this.endpoint.toLowerCase(),
      accessKey: this.accessKey,
      secretKey: this.secretKey,
      regionName: this.regionName
    };
    var client = this.clients.getClient(options);
    if (!client.connected) {
      client.connect(options);
    }
};

My javascript

<body>
    <div id='myid' class="container" ng-app="awsiot.sample" ng-controller="AppController as vm">
  ....
  <div class="form-group">
    <button class="btn btn-primary" id='myclick' ng-click="vm.createClient()" >Create Client</button>  <-- working! -->      
  </div>  

....

<script>
  angular.element('#myid').scope().createClient(); // not working!
</script>

Can you help me?

3
  • Based on script tag shown you might be calling that before angular has even compiled the controller and the dom. Any errors in console? Commented Dec 13, 2018 at 20:07
  • Looking up elements via selectors is not supported by angular.element. Also controller scope is created during the AngularJS run phase which occurs well after DOMContentLoaded. This looks like an XY Problem. What are you trying to accomplish here? Maybe there is a better way than calling a controller function from outside the AngularJS framework. Commented Dec 13, 2018 at 20:56
  • @georgeawg To call createClient() in angular is what I want to accomplish :) Commented Dec 22, 2018 at 12:54

2 Answers 2

1

try this:

angular.element(document.querySelector('#myid')).controller().info();

demo https://stackblitz.com/edit/angularjs-wxjhgd you can tweak it to suit your needs

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

3 Comments

“While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.”
I already included the essential part ... Other parts are straightforward :)
Thanks ABOS I tried but the result is, (index):117 Uncaught TypeError: Cannot read property 'info' of undefined at (index):117 (anonymous) @ (index):117
0

To call createClient() in angular is what I want to accomplish

Put the call in the $onInit function of the controller:

function AppController() {
    this.$onInit = function() {
        this.createClient();
    });
}

For more information, see AngularJS $compile Service API Reference - Life-Cycle Hooks.

Comments

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.