# 依赖和注入
### [angular-resource](https://github.com/angular/bower-angular-resource)
bower install angular-resource@1.5.3 --save
angular.module('myApp', ['ngResource']);
### [angular-underscore-module](https://github.com/andresesfm/angular-underscore-module)
bower install angular-underscore-module --save
angular.module('myApp', ['underscore'])
### config
$resourceProvider.defaults.stripTrailingSlashes = false;
$resourceProvider.defaults.actions.update = {
method: 'PUT',
params: {
id: "@id"
}
};
$resourceProvider.defaults.actions.patch = {
method: 'PATCH',
};
# list
## controller
var task = $resource('url/:id/');
task.get(function (res) {
$scope.taskList = res;
}, function (err) {
alert(JSON.stringify(err));
})
$scope.toDetail = function (item) {
Task.detail = _.clone(item);
$state.go('tab.task-detail',{
id:item.id
})
}
$scope.toAdd = function () {
$state.go('tab.task.detail',{
id:0
})
}
## html
1.title
2.list
{{item.title}} {{item.created | date:'yyyy/MM/dd' }}
{{item.description}}
## doRefresh
function getData() {
Task.getTask.get(function (res) {
$scope.taskList = res;
}, function (err) {
alert(JSON.stringify(err));
}).$promise.finally(function () {
$scope.$broadcast('scroll.refreshComplete');
});
}
## config timestamp
$httpProvider.interceptors.push(function () {
return {
'request': function (config) {
config.url += (config.url.indexOf("?") === -1 ? "?" : "&") + "v=" + Date.now();
return config;
}
};
});
# details
## controller
1.route
.state('tab.task-detail', {
url: '/account/:id',
views: {
'tab-account': {
templateUrl: 'templates/task-detail.html',
controller: 'TaskDetailCtrl'
}
}
});
2.factory
.factory('Task', function ($resource) {
var task = {};
task.detail = {};
task.getTask = $resource('url/:id/');
return task;
})
3.controller
var id = $stateParams.id;
var isNew = id == 0 ? true : false;
$scope.data = isNew ? {
title:null,
description:null
} : _.clone(Task.detail);
$scope.addTask = function () {
if (isNew) {
task.save($scope.data,function () {
$ionicHistory.goBack();
});
} else {
Task.getTask.put($scope.data,function () {
$ionicHistory.goBack();
});
}
}
$scope.deleteTask = function () {
Task.getTask.delete({id:id});
}
## html