angular-data 是 AngularJS 的一个扩展,实现了数据存储和缓存的功能。
示例代码:
var app = angular.module('myApp', ['angular-data.DS']); app.factory('User', function (DS) { // Simplest resource definition return DS.defineResource('user'); }); app.controller('friendsCtrl', function ($scope, $routeParams, User) { // it's up to your server to know how to interpret this query // or you can teach angular-data how to understand your servers' query language var query = { where: { friendIds: { in: $routeParams.id } } }; User.find($routeParams.id); User.findAll(query); // My goodness this was easy User.bindOne($scope, 'me', $routeParams.id); User.bindAll($scope, 'friends', query); // Long form $scope.$watch(function () { return User.lastModified($routeParams.id); }, function () { $scope.me = User.get($routeParams.id); }); $scope.$watch(function () { // Changes when anything in the User collection is modified return User.lastModified(); }, function () { $scope.friends = User.filter(query); }); });