Skip to content

Instantly share code, notes, and snippets.

Created March 28, 2015 01:55
Show Gist options
  • Save anonymous/33b61b6ba4e21d08265a to your computer and use it in GitHub Desktop.
Save anonymous/33b61b6ba4e21d08265a to your computer and use it in GitHub Desktop.
//factory
app.factory('dataLoad', function($http, $q) {
return {
getData: function() {
var deferred = $q.defer();
$http.get('data-json.php')
.success(function(data, status, headers, config) {
deferred.resolve(data);
})
.error(function(data, status, headers, config) {
deferred.reject();
});
return deferred.promise;
}
}
});
//controller
$scope.items = [];
$scope.get = function () {
$scope.items = dataLoad.getData();
$scope.items.then(function (data) {
//nulo
$scope.items = data;
//okay
console.log(data);
}, function (status) {
console.log(status);
});
};
$scope.get();
console.log($scope.items);
@nicholasess
Copy link

Você pode fazer o seguinte:
//factory
app.factory('dataLoad', function($http) {
return {
getData: function() {
return $http.get('data-json.php')
}
}
});

//controller
$scope.items = [];
dataLoad.getData().then(function(data){
//deu certo
$scope.items = data;
}, function(data){
//deu errado
});

Veja essa serie que eu criei com angularjs e nodejs, pode te ajudar.
http://nicholasess.com.br/desenvolvimento/crud-com-angularjs-e-nodejs-serie/

Abraços

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment