简单的情况,我有一个服务来检索一个名为lists(<list_id>).
但我不明白如何自动调用该服务......
JS Fiddle 我的问题
<select name='lists'
ng-model="list"
ng-options="t.title for t in lists._lists">
<option value="">-- chose list --</option>
</select>
<h1>List was -{{ list.title }}</h1>
<ul>
<li ng-repeat="t in lists.lists(list)" class='task'>{{t.title}}: {{t.id}}</li>
</ul>
主要服务
angular.module('service',['ngResource']).factory('List', function($http) {
var List= function(data) {
#magic to build List from json
#save to ._lists
#or if just one build List
}
List.lists = function(list_id, query) {
url = '/tasks/lists/';
#if list_id is there use it else give all lists
return $http.get(url).then(function(response) {
return new List(response.data);
});
};
主要js
//this initially populates lists._lists with all the available lists
//it also has a List method on the object
$scope.lists= List.lists();
当我更改选择时,我可以看到标题更新。我的控制器有一个“列表”方法,它接受一个列表对象,并会执行查询,但它永远不会被调用。
关于我所缺少的任何建议?