简体   繁体   English

嵌套的角度控制器和视图管理

[英]Nested Angular Controllers and Views management

I guess it is best to describe it with a picture. 我想最好用图片来描述它。 I have an angular app and here is a simple view. 我有一个角度应用程序,这是一个简单的视图。

基本观点

Obvious explanation: list shows all the entities, if you click on an entity you can edit it in the form that is hidden by default and similar action applies to adding a new entity. 明显的解释:列表显示所有实体,如果单击一个实体,则可以默认情况下隐藏的形式对其进行编辑,并且类似的操作适用于添加新实体。

the issue 问题

I know it is basic example so here the solution might be an overkill but I want to separate the logic of 'Add new entity', 'Edit entity' and 'Entities list'. 我知道这是一个基本示例,因此这里的解决方案可能有点过分,但是我想将“添加新实体”,“编辑实体”和“实体列表”的逻辑分开。 I thought I could implement it like this: 我以为我可以这样实现:

<div ng-include="'userAddForm.html'"
     ng-show="???"
     ng-controller="AddUser as add">
</div>
<div ng-include="'userEditForm.html'"
     ng-show="???"
     ng-controller="AddEdit as edit">
</div>

<div class="panel panel-default">
   ... list managed by the current controller
</div>

What I miss 我想念的

I have a difficulty in sharing a state of the hidden parts. 我很难共享隐藏部分的状态。 For example some boolean flag. 例如一些布尔标志。 For instance: 例如:

  1. Click on the entity shows the edit form 单击实体显示编辑表单
  2. Save/Cancel in the edit form hides the part 在编辑表单中保存/取消隐藏零件

Then, I think the first step is the responsibility of list-controller, but save/cancel part goes to edit-controller. 然后,我认为第一步是列表控制器的责任,但是保存/取消部分交给编辑控制器。 It would be only possible to share the value with a service included in both - but that does not seem reasonable either. 与两者中包含的服务共享价值是可能的-但这似乎也不合理。

I think there is some simple solution I can not see and I am open for any advice. 我认为有一些我看不到的简单解决方案,我愿意接受任何建议。 Thanks! 谢谢!

If your goal is a simple solution with just a boolean being toggled in the model, you can use child controllers like this: 如果您的目标是一个简单的解决方案,只需在模型中切换一个布尔值,则可以使用以下子控制器:

http://plnkr.co/edit/P1ncToJwqvxt9F9MTF5E?p=preview http://plnkr.co/edit/P1ncToJwqvxt9F9MTF5E?p=preview

The child controllers will inherit the scope of the parent controller and can directly edit the values. 子控制器将继承父控制器的范围,并可以直接编辑值。 I have the edit child controller filtering for editMode==true, so when the parent changes that value, the child controller automatically shows the item. 我的编辑子控制器过滤了editMode == true,因此当父控制器更改该值时,子控制器会自动显示该项目。 All changes are updated live and the child controller simply toggles the editMode property to remove it from the editing area. 所有更改都将实时更新,并且子控制器只需切换editMode属性即可将其从编辑区域中删除。

Similar logic is used for the add child controller. 类似的逻辑用于添加子控制器。

The views look like this: 视图如下所示:

index.html index.html

<div ng-controller="myCtrl">

  <div ng-controller="addCtrl" ng-include="'userAddForm.html'">
  </div>
  <div ng-controller="editCtrl" ng-include="'userEditForm.html'">
  </div>
    <h1>Listing</h1>
    <ul>
        <li ng-repeat="item in items |  filter:{addMode:false}">
            {{item.id}}   
            {{item.name}}  

            <button ng-click="startEditing(item)">[ edit ]</button>
        </li>
    </ul>
    <button ng-click="startAdding()">[ add ]</button>
  <div>Debug:<br>{{items}}</div>
</div>

userAddForm.html userAddForm.html

<ul>
    <li ng-repeat="item in items | filter:{addMode:true}">
        <input type="text" ng-model="item.id">
        <input type="text" ng-model="item.name">
          <button ng-click="add(item)">[ add ]</button>
          <button ng-click="cancel(item)">[ cancel ]</button>
    </li>
</ul>

userEditForm.html userEditForm.html

<ul>
    <li ng-repeat="item in items | filter:{editMode:true}">
      <input type="text" ng-model="item.id">
      <input type="text" ng-model="item.name">
        <button ng-click="save(item)">[ save ]</button>
    </li>
</ul>

And the controllers look like this: 控制器看起来像这样:

angular.module('myApp.controllers',[])
  .controller('addCtrl', function($scope) {
    $scope.add = function(item) {
      item.addMode = false;
    }
    $scope.cancel = function(item) {
      $scope.items.pop(item);
    }
  })
  .controller('editCtrl', function($scope) {
    $scope.save = function(item) {
      item.editMode = false;
    }
  })
  .controller('myCtrl', function($scope) {

        $scope.items = [
            {name:'aap', id:"1", editMode:false, addMode:false},    
            {name:'noot', id:"2", editMode:false, addMode:false},    
            {name:'mies', id:"3", editMode:false, addMode:false},    
            {name:'zus', id:"4", editMode:false, addMode:false}
        ];


        $scope.startAdding = function(){
          $scope.items.push({addMode:true});
        };

        $scope.startEditing = function(item){
          item.editMode = true;
        };

  });

You can achieve this using Angular state routing.In which you will create state (different views) like - 您可以使用Angular状态路由来实现此目的。在其中您将创建状态(不同的视图),例如-

header addEntity editEntity listEntity 标头addEntity editEntity listEntity

refer https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views 参考https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views

Sharing state can be implemented by creating a service which is than injected to all interested párties (controllers), service can hold data which controllers can be bound to and display in template. 共享状态可以通过创建一个服务来实现,然后将服务注入所有感兴趣的部分(控制器),该服务可以保存控制器可以绑定到的数据并显示在模板中。 Services in Angular JS are singletons so all the controllers are accesing and mutating shared state. Angular JS中的服务是单例,因此所有控制器都在访问和更改共享状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM