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

时间:2017-07-11 05:51:22

标签: javascript angularjs

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

控制器

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

1 个答案:

答案 0 :(得分:2)

你可以像这样使用

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];


}]); 

Angular App:

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

和HTML文件:

Private Sub CmdAddReps3_Click()
Dim iRepNo As Integer   ' stores the current value in the series

'Open the table
Set db = CurrentDb()
Set rstGReps = db.OpenRecordset("tblGReplicates")

' Initialise the variables
iRepNo = 1
iNoofReps = 3   'iNoofReps = Me.txtNoofReps

' Add the records using a loop
    rstGReps.movefirst

Do 'Until rstGReps("RepNo") = (iNoofReps + 1) ' always want to include at least 1 repNo

rstGReps.AddNew
rstGReps("GTestID") = Me.GTestID
rstGReps("RepNo") = iRepNo
rstGReps("NoofSeed") = Me.txtNoOfSeeds

' Calculate the next RepNo value in the loop
iRepNo = iRepNo + 1
rstGReps.Update
rstGReps.moveNext

Loop Until rstGReps("RepNo") = (iNoofReps) + 1 ' so that the loop includes the final repNo.

MsgBox "Finished Looping"
rstGReps.Close

Set rstGReps = Nothing
Set db = Nothing

End Sub