2016-04-06 16 views
0

Web sayfam, sunucudaki dosyaların bir listesini gösterir ve dosya adının yanında bir "install" düğmesi bulunur. "Yükle" yi tıklattığımda, sunucu yüklenecek olan yanıtlama dosyasının adını almalıdır. Geçerli kodum, dosyanın adını sunucuya psas edemiyor. Düğme tıklamasıyla sunucuya dosya yollamanın nasıl yapıldığını önerebilir misiniz?Düğmeyle sunucu tarafından yüklenecek dosyanın adının gönderilmesi angularjs'ye tıklayın

İşte benim kod

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>

cevap

1

Bir servise $scope geçemezse gerektiğidir. Ancak dosya adını doğrudan iletebilirsiniz. Bunu deneyin:

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

Ve HTML'niz bununla çalışmak için iyi görünüyor. $ Http dönüşünden sonra herhangi bir işlem yapmanız gerekiyorsa, bunu RdaService.SendFileToCTP çağrısında .then() ile yapabilirsiniz.

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

Çok teşekkürler:

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

1
böyle RdaService.SendFileToCTP($scope.file).then() kullanmak zorunda söz $ http sonucunu elde etmek için! Bu çalışıyor
İlgili konular