通过按钮点击angularjs

时间:2016-04-06 20:34:07

标签: javascript angularjs angularjs-scope

我的网页显示服务器上的文件列表,文件名旁边是"安装"按钮。当我单击"安装"时,服务器应该收到要安装的相应文件的名称。我当前的代码无法将文件名称psas到服务器。您能否建议如何在按钮点击时将文件名发送到服务器?

这是我的代码



app.controller('RdaController', ['$scope', 'RdaService',
  function($scope, RdaService) {
    $scope.greeting = "Hello world";

    $scope.file = "installJob.zip";
    $scope.sendToCTP = function($scope) {
      return RdaService.SendFileToCTP($scope);
    };
  }
]);


app.service('RdaService', ['$http',
  function($http) {
    this.SendFileToCTP = function($scope) {
      return $http({
        method: "GET",
        url: "../api/CTP/installJobFromFile/" + $scope.file,
        headers: {
          'Content-Type': 'application/json'
        }
      }).success(function(data) {
        //$scope.sendToCTP = data;
        console.log(data);
      }).error(function(data) {
        console.log(data);
      });
    };
  }
]);

<h2>List of Files available</h2>
<table class="table table-striped">
  <tr ng-repeat="file in listOfFiles">
    <td>{{ file }}</td>
    <td><span class="btn-group" role="group"><button type="button" class="btn btn-default" ng-click="sendToCTP(file)">install</button><button type="button" class="btn btn-default">delete</button></span>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您不应将$scope传递给服务。但是,您可以直接传递fileName。试试这个:

app.controller('RdaController', ['$scope', 'RdaService',
  function($scope, RdaService) {
    $scope.greeting = "Hello world";

    $scope.file = "installJob.zip";
    $scope.sendToCTP = function(file) {
      return RdaService.SendFileToCTP(file);
    };
  }
]);


app.service('RdaService', ['$http',
  function($http) {
    this.SendFileToCTP = function(file) {
      return $http({
        method: "GET",
        url: "../api/CTP/installJobFromFile/" + file,
        headers: {
          'Content-Type': 'application/json'
        }
      }).success(function(data) {

        console.log(data);
      }).error(function(data) {
        console.log(data);
      });
    };
  }
]);

你的HTML看起来可以使用它。如果您需要在$ http返回后执行任何操作,则可以使用RdaService.SendFileToCTP调用上的.then()执行此操作。

答案 1 :(得分:1)

app.service('RdaService', ['$http',
  function($http) {
    this.SendFileToCTP = function(filename) {
      return $http({
        method: "GET",
        url: "../api/CTP/installJobFromFile/" + filename,
        headers: {
          'Content-Type': 'application/json'
        }
      }).success(function(data) {
        console.log(data);
        // DONT FORGET TO RETURN DATA HER
        return data ;
      }).error(function(data) {
        console.log(data);
        return null ;
      });
    };
  }
]);

要获得$ http承诺的结果,您必须使用RdaService.SendFileToCTP($scope.file).then(),如下所示:

app.controller('RdaController', ['$scope', 'RdaService',
  function($scope, RdaService) {
    $scope.greeting = "Hello world";

    $scope.file = "installJob.zip";
    $scope.sendToCTP = function($scope.file) {
        RdaService.SendFileToCTP($scope.file).then(function (res) {
            // HER you can use the result of your callback on $http 
            console.log(res);
            $scope.result = res ; // for exemple
        });
    };
  }
]);
相关问题