我无法理解这段代码的工作流程
I can't understand the workflow of this code
我正在做一个在线教程,他们教你使用下面的 MEAN.The 代码制作一个简单的网络应用程序,用于编辑给定的 JSON 对象集合(视频是 JSON 对象在这里)
该系列位于
/api/videos
因此,我必须单击 href="/#/video/{{video._id}}
,这会将我带到 form.html,并且我可以选择编辑 JSON 对象的 'title' 和 'description' 参数。
我似乎无法理解的是:
a)为什么需要这个(问题下方的完整代码)
var Videos = $resource('/api/videos/:id', { id: '@_id' },
{
update: { method: 'PUT' }
});
因为我在 href="/#/video/{{video._id}}
上,我不能直接从 URL
中获取 ID
var Videos=$resource('api/videos)
Videos.get({ id: $routeParams.id }, function(video){
$scope.video = video;
});
b)Whait 是工作流(即什么时候 router.get() 请求是什么时候发出的,什么时候是 router.put() 请求发出的)
根据我的说法,当我单击保存按钮时,控制器向 API 发出了一个放置请求,但我不知道何时发出 router.get() 请求
我正在尝试阅读 express 和 angular 文档,但它们似乎没有解释工作流程。
您能否也告诉我应该阅读哪些内容才能更好地理解?
这是form.html代码
<h1>Add a Video</h1>
<form>
<div class="form-group">
<label>Title</label>
<input class="form-control" ng-model="video.title"></input>
</div>
<div>
<label>Description</label>
<textarea class="form-control" ng-model="video.description"></textarea>
</div>
<input type="button" class="btn btn-primary" value="Save" ng-click="save()"></input>
</form>
这是控制器代码
app.controller('EditVideoCtrl', ['$scope', '$resource', '$location', '$routeParams',
function($scope, $resource, $location, $routeParams){
var Videos = $resource('/api/videos/:id', { id: '@_id' },
{
update: { method: 'PUT' }
});
Videos.get({ id: $routeParams.id }, function(video){
$scope.video = video;
});
$scope.save = function(){
Videos.update($scope.video, function(){
$location.path('/');
});
}
}]);
这是API端点代码
router.get('/:id', function(req,res){
var collection =db.get('videos');
collection.findOne({_id: req.params.id},function(err,video){
if(err) throw err;
res.json(video);
});
});
router.put('/:id', function(req, res){
var collection=db.get('videos');
collection.update({_id:req.params.id},
{title: req.body.title,
description: req.body.description
},
function (err,video)
{if (err) throw err;
res.json(video);
});
});
嗯,根据 AngularJS docs for $resouce,$resource 是:
A factory which creates a resource object that lets you interact with
RESTful server-side data sources.
换句话说就是RESTful服务操作的快捷方式。下面的代码创建了一个带有 API 端点的接口,使 REST 操作更容易完成。
一旦你有了这个:
var User = $resource('/user/:userId', {userId:'@id'});
这样做更容易:
User.get({userId:123}, function(user) {
user.abc = true;
user.$save();
});
因为RESTful是一个标准,而$resource
是Angular在这个标准中消耗API的实现。在他的内部,根据您配置和选择的操作,使用propper headers和方法发出异步请求。
我正在做一个在线教程,他们教你使用下面的 MEAN.The 代码制作一个简单的网络应用程序,用于编辑给定的 JSON 对象集合(视频是 JSON 对象在这里)
该系列位于
/api/videos
因此,我必须单击 href="/#/video/{{video._id}}
,这会将我带到 form.html,并且我可以选择编辑 JSON 对象的 'title' 和 'description' 参数。
我似乎无法理解的是:
a)为什么需要这个(问题下方的完整代码)
var Videos = $resource('/api/videos/:id', { id: '@_id' },
{
update: { method: 'PUT' }
});
因为我在 href="/#/video/{{video._id}}
上,我不能直接从 URL
var Videos=$resource('api/videos)
Videos.get({ id: $routeParams.id }, function(video){
$scope.video = video;
});
b)Whait 是工作流(即什么时候 router.get() 请求是什么时候发出的,什么时候是 router.put() 请求发出的) 根据我的说法,当我单击保存按钮时,控制器向 API 发出了一个放置请求,但我不知道何时发出 router.get() 请求
我正在尝试阅读 express 和 angular 文档,但它们似乎没有解释工作流程。 您能否也告诉我应该阅读哪些内容才能更好地理解?
这是form.html代码
<h1>Add a Video</h1>
<form>
<div class="form-group">
<label>Title</label>
<input class="form-control" ng-model="video.title"></input>
</div>
<div>
<label>Description</label>
<textarea class="form-control" ng-model="video.description"></textarea>
</div>
<input type="button" class="btn btn-primary" value="Save" ng-click="save()"></input>
</form>
这是控制器代码
app.controller('EditVideoCtrl', ['$scope', '$resource', '$location', '$routeParams',
function($scope, $resource, $location, $routeParams){
var Videos = $resource('/api/videos/:id', { id: '@_id' },
{
update: { method: 'PUT' }
});
Videos.get({ id: $routeParams.id }, function(video){
$scope.video = video;
});
$scope.save = function(){
Videos.update($scope.video, function(){
$location.path('/');
});
}
}]);
这是API端点代码
router.get('/:id', function(req,res){
var collection =db.get('videos');
collection.findOne({_id: req.params.id},function(err,video){
if(err) throw err;
res.json(video);
});
});
router.put('/:id', function(req, res){
var collection=db.get('videos');
collection.update({_id:req.params.id},
{title: req.body.title,
description: req.body.description
},
function (err,video)
{if (err) throw err;
res.json(video);
});
});
嗯,根据 AngularJS docs for $resouce,$resource 是:
A factory which creates a resource object that lets you interact with RESTful server-side data sources.
换句话说就是RESTful服务操作的快捷方式。下面的代码创建了一个带有 API 端点的接口,使 REST 操作更容易完成。 一旦你有了这个:
var User = $resource('/user/:userId', {userId:'@id'});
这样做更容易:
User.get({userId:123}, function(user) {
user.abc = true;
user.$save();
});
因为RESTful是一个标准,而$resource
是Angular在这个标准中消耗API的实现。在他的内部,根据您配置和选择的操作,使用propper headers和方法发出异步请求。