Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将外部类调用到AngularJS控制器中_Javascript_Angularjs - Fatal编程技术网

Javascript 如何将外部类调用到AngularJS控制器中

Javascript 如何将外部类调用到AngularJS控制器中,javascript,angularjs,Javascript,Angularjs,我在外部javascript类中有一个方法。我正在尝试将该方法调用到我的控制器函数中。(在我的视图中有一个切换按钮,当选中切换时,我想调用外部方法) 控制器 myApp.factory('PushClient', ['', function() { return PushClient; }]); myApp.controller('MainController', function (PushClient,$scope, $rootScope, $http, $window){ $

我在外部javascript类中有一个方法。我正在尝试将该方法调用到我的控制器函数中。(在我的视图中有一个切换按钮,当选中切换时,我想调用外部方法)

控制器

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 {

    }
  };

}
外部PushClient类

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

    mypush(){
        alert("hiiii");
    }
}
你可以这样用

Class:

class Client {

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

}
工厂:

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


  return new Client();

}]);
Angular应用程序:

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

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

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


}]); 
和HTML文件:

<body ng-app="testApp" ng-controller="myCtrl">
    <h1>Hello Plunker!</h1>
     <ul>
        <li ng-repeat="x in products">{{x}}</li>
    </ul>
  </body>

你好,普朗克!
  • {{x}

请参见此处,您是如何导入文件类客户端的?