javascript - Angular $http.get -
i'm trying show lists , tasks associated each list. in controller have:
$http.get('api/list/').success(function (data) { $scope.lists = data; $scope.tasks = data[0].task; });
this works first item of course data[0].task needs dynamic. problem i'm having being called once each list. tried using variable gets reset it's original value. i've tried using callback no luck. i'm not sure i'm overlooking or if i'm going wrong.
your get api/list/
request return this:
[ { "id": 1, "name": "list #1", "tasks": [ { "id": 1, "name": "task #1 on list #1" }, { "id": 2, "name": "task #2 on list #1" }, { "id": 3, "name": "task #3 on list #1" } ] }, { "id": 2, "name": "list #2", "tasks": [ { "id": 1, "name": "task #1 on list #2" }, { "id": 2, "name": "task #2 on list #2" }, { "id": 3, "name": "task #3 on list #2" } ] } ]
this assuming want return associated tasks in api/list/
command.
you need call once every time want refresh lists , tasks.
you should have single controller bound view, in $http.get called. should set $scope.lists = data
on success.
in view need 2 nested ng-repeat
tags. example, use unordered lists:
<div ng-controller="listscontroller"> <ul> <li ng-repeat="list in lists"> <ul> <li ng-repeat="task in list.tasks"> </li> </ul> </li> </ul> </div>
i haven't used angular i'm pretty sure need do. single ajax call populate <li>
element each list, nested <li>
elements each task belonging list.
Comments
Post a Comment