Answer the question
In order to leave comments, you need to log in
How to make an http request to the factory?
Hello.
Started rewriting the project while it was at an early stage, but ran into difficulty.
The controller has a function that is called by clicking on the link, receives json, which it puts into an array. Further, this data is displayed on the page using ng-repeat and the directive.
$scope.getFiles = function(id) {
$element.find("files").remove(); // очистка div'a
$http.get("http://тут_урл").success(function(response) {
$scope.files = response;
});
};
app.directive("file", function() {
return {
restrict : "E",
scope : true,
controller : function($scope, $element) {
$scope.fileName = $scope.file.fileName;
$scope.fileContent = $scope.file.content;
},
link : function(scope, el, attr) {
scope.fileName = scope.fileName;
scope.fileContent = scope.fileContent;
},
template : "<div id=\"{{fileName}}\"><h3 class='title'>name: {{fileName}}</h3> \
<pre class=\"java\">{{fileContent}}</pre></div>"
}
});
angular.module("myApp").factory("gistsFactory", function( $http) {
var service = {};
service.getFiles = function(id) {
// $element.find("files").remove();
$http.get("http://тут_урл").success(function(response) {
return response;
});
}
return service;
})
//В контроллере меняю на вызов функции
$scope.getFiles = function(id) {
$scope.files = gistsFactory.getFiles(id);
};
Answer the question
In order to leave comments, you need to log in
to the factory
.factory("gistsFactory", function( $http, $q) {
return {
getFiles: function(id) {
return $http.get("http://тут_урл").then(function(response) {
return response.data;
}, function (reason) {
return $q.reject(reason);
});
};
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question