0

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.

6
  • how will be your data look like? Commented Oct 22, 2016 at 13:41
  • also add your api call implementation Commented Oct 22, 2016 at 13:42
  • 2
    can send tel about $rootScope.data? whr u are saving and assigning? Commented Oct 22, 2016 at 13:43
  • @sravan the data will be in json format. Commented Oct 22, 2016 at 13:47
  • it is an object, or an array? Commented Oct 22, 2016 at 13:55

1 Answer 1

1

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);
      });
  };
};  
Sign up to request clarification or add additional context in comments.

6 Comments

explain please. code only answers are not very helpful.
yes once, he implements this and comment something I can explain, any way I am adding the explanation
@cale_b, added some explanation, anyway thnk you for your suggestion.
$scope tasks logs undefined, might have something to do with how the data is structured?
exactly, data should be in [ {"id": 1, "name": "Test1"} {"id": 2, "name": "Test2"} {"id": 3, "name": "Test3"} ] this format
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.