Angular JS – 区分保存和更新,使用共同的模板

huangapple go评论59阅读模式
英文:

Angular JS - differentiating between a save and update with a common template

问题

我正在使用PHP后端和Angular 1.x前端开发项目。我有一个名为Listings的模型,并且我使用一个通用模板来创建(添加)和更新(编辑)Listing。

由于编辑和添加都使用相同的前端模板,我需要能够检测用户是在创建还是编辑。似乎有几种方法可以做到这一点:

  1. 我可以在ng-submit中传递一个参数:
ng-submit="saveListing({{isNewListing}})"

然后我可以在我的控制器中读取参数的值,但我认为这可能有点繁琐?

  1. 当编辑Listing时,某些变量被设置为表单的自动填充:
$scope.listing.id = x;

因此,我只需检查上述变量中是否有值:

$scope.saveListing = function() {
    if(listing.id) {
         // 更新操作
    } else {
         // 保存操作
    }
};

第二种选项是否是一个合理且不会有问题的方法?我不是Angular专家,所以尽管这对我来说似乎是合乎逻辑的方法,但我想确保我没有出现问题。

英文:

I am working on a project with a PHP backend and Angular 1.x on the front end. I have a Listings model and I use a common template to create (add) and update (edit) a Listing.

Since eidt and add both use the same front end template I need to be able to detect when a user is creating and when they are editing. It seems there are several ways to do this:

  1. I could pass a paramater in the ng-submit:

     ng-submit="saveListing({{isNewListing}}"
    

Then I could read the value of the paramter in my controller, but I think this is overkill?

  1. When editing a Listing some variables set for the form auto-fill

     $scope.listing.id = x;
    

Therfore I could just check for a value in the above:

$scope.saveListing = function() {
    if(listing.id) {
         // update action
    } else {
         // save action
    }
};

Is the second option a sound and non-hacky approach. I am not an Angular pro so although it seems the logical approach to me I want to ensure that I am not hot woring this.

答案1

得分: 2

我通常采用类似第二种方法的方式。由于编辑意味着您通常需要“获取”原始记录,因此记录应该在作用域中的某个位置存在。我使用了ui-router,并且为记录设置了一个解析器,这意味着我可以在控制器的顶部立即进行检查:

$scope.isEdit = record != null;

通过使用作用域变量或类似的方式(例如controllerAs vm),您可以利用您处于“编辑模式”并稍微改变界面的事实。而不是在按钮上显示“+ 新建”,您可以显示“+ 保存”。

希望有所帮助!

英文:

I usually do something similar to the second approach. Since editing means you have to "get" the original record in most cases, the record should exist somewhere in the scope. I use ui-router and have a resolve for the record, which means I can check right at the top of the controller:

$scope.isEdit = record != null;

With a scope variable or similar (e.g. controllerAs vm) you can leverage the fact that you're in "edit mode" and change the UI up a bit. Instead of "+ New" on a button you can have "+ Save".

Hope that helps!

答案2

得分: 0

我们有一个大型的ERP系统,使用AngularJS作为前端框架,我们采用了“检查ID”方法。
在更新/编辑项目时,该项目将具有现有的ID。
我认为第二种方法很好,我没有看到任何缺点。

英文:

We have a large ERP system with angularJs as a front-end framework, and we are using the "check id" approach.
When updating/edit an item there would be existing id for that item.
I think the second approach is good and I don't see any drawbacks.

huangapple
  • 本文由 发表于 2020年1月3日 22:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定