Created
March 28, 2015 01:55
-
-
Save anonymous/33b61b6ba4e21d08265a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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