Reputation: 395
Found a code doing a to do list.
$scope.tasks = [];
$scope.searchEnter = function() {
if(event.which == 13 && $scope.task !="") {
$scope.addTask();
};
};
$scope.addTask = function() {
$scope.tasks.push({'taskMessage':$scope.task, 'done':false});
console.log($scope.tasks);
$scope.task = "";
};
$scope.clearCompleted = function() {
$scope.tasks = $scope.tasks.filter(function(item){
return !item.done;
});
};
Was thinking about using it in a project where data is gathered from an API call.
How should data fetched with an API call be implemented into the to do list function? The data is in $rootScope.data and lets say it should be possible to store an attribute like data.id in the to do list.
Tried something like this without any results.
<tr ng-repeat="x in data.list">
<button ng-click="add()">Add!</button>
<td>
<input type="checkbox" ng-model="x.done" />
{{ x.id }}
</td>
API call
$scope.q ="";
$scope.searchData = function(url, id, callBack, apiCall, promise) {
var url="http://api.openweathermap.org/data/2.5/find?q="+$scope.q+"&type=like";
var id = APPKEY;
var callBack = "&callback=JSON_CALLBACK";
var apiCall = url + id + callBack;
if($scope.q.length >= 3) {
var promise = $http.jsonp(apiCall);
promise.success(function(data){
$rootScope.data = data;
console.log($rootScope.data);
});
};
Want the users to be able to pick attributes and store them by their own liking.
Upvotes: 0
Views: 381
Reputation: 18647
On promise success function,you assigned data
to $rootScope.data
.
What I have done is loop through that data and send every chunk of data to the addTask
method.
That method pushes the data into the tasks array
which you are displaying in the view.
As you said, I am assuming your response contains, array of objects.
Update: Data returned from promise should be something like this,
[
{"id": 1, "name": "Test1"}
{"id": 2, "name": "Test2"}
{"id": 3, "name": "Test3"}
]
$scope.q ="";
$scope.searchData = function(url, id, callBack, apiCall, promise) {
var url="http://api.openweathermap.org/data/2.5/find?q="+$scope.q+"&type=like";
var id = APPKEY;
var callBack = "&callback=JSON_CALLBACK";
var apiCall = url + id + callBack;
if($scope.q.length >= 3) {
var promise = $http.jsonp(apiCall);
promise.success(function(data){
$rootScope.data = data;
$scope.tasks = []
$scope.addTask = function(data) {
$scope.tasks.push(dara);
console.log($scope.tasks);
};
for (var i = 0; i < $rootScope.data.length; i++)
{
$scope.addTask($rootScope.data[i])
}
$scope.addTask();
console.log($rootScope.data);
});
};
};
Upvotes: 1