Javascript 如何将外部类调用到AngularJS控制器中
我在外部javascript类中有一个方法。我正在尝试将该方法调用到我的控制器函数中。(在我的视图中有一个切换按钮,当选中切换时,我想调用外部方法) 控制器Javascript 如何将外部类调用到AngularJS控制器中,javascript,angularjs,Javascript,Angularjs,我在外部javascript类中有一个方法。我正在尝试将该方法调用到我的控制器函数中。(在我的视图中有一个切换按钮,当选中切换时,我想调用外部方法) 控制器 myApp.factory('PushClient', ['', function() { return PushClient; }]); myApp.controller('MainController', function (PushClient,$scope, $rootScope, $http, $window){ $
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}
请参见此处,您是如何导入文件类客户端的?