1

I have a method inside an external javascript class.i'm trying to call that method inside to my controller function.(there is a toggle button in my view,when toggle is checked,i want to call external method)

Controller

myApp.factory('PushClient', ['', function() {

    return PushClient;
}]);

myApp.controller('MainController', function (PushClient,$scope, $rootScope, $http, $window){

$rootScope.toggleSelection = function toggleSelection(event) {

    if(event.target.checked){

      var PushClient = new PushClient();
      PushClient.mypush();
    }else {

    }
  };

}

external PushClient class

class PushClient {
constructor(....){...}
       ............

    mypush(){
        alert("hiiii");
    }
}
1

1 Answer 1

2

you can use like this

Class:

class Client {

   get Name() {
    return this.mypush();
  }
    mypush(){
        return "Imran";
    }

}

Factory :

app.factory('PushClient', [ function() {


  return new Client();

}]);

Angular App :

var app = angular.module("testApp", []);

app.controller("myCtrl",['$scope','PushClient' ,function($scope,PushClient) {

    $scope.products = ["Milk", "Bread", "Cheese",PushClient.Name];


}]); 

and HTML file :

<body ng-app="testApp" ng-controller="myCtrl">
    <h1>Hello Plunker!</h1>
     <ul>
        <li ng-repeat="x in products">{{x}}</li>
    </ul>
  </body>
Sign up to request clarification or add additional context in comments.

1 Comment

How did you import the file class Client ?

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.