2017-07-11 30 views
1

我在外部JavaScript類中有一個方法,我試圖調用裏面的方法來調用我的控制器函數(在我的視圖中有一個切換按鈕,當切換爲checked時,我想調用外部方法)如何將外部類調用到AngularJS控制器中

控制器

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"); 
    } 
} 
+0

在這裏看到https://開頭developer.mozilla.org/ EN /文檔/網絡/的JavaScript /參考/班 – gdmanandamohon

回答

2

你可以使用這樣

Class: 

class Client { 

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

} 

廠:

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


    return new Client(); 

}]); 

角應用:

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> 
相關問題