J
J
JIakki2016-01-18 17:11:32
Angular
JIakki, 2016-01-18 17:11:32

How to get rid of controllers?

I have several pages in which almost everything is identical, except for these and a few other methods
. If I create a Directive, I do not know how to add a different method to each page, and if I create a Controller, then almost everything is repeated (except for those one or two methods)
What is the correct way to use the Controller of Directives, so as not to create several almost identical controllers?
Thanks in advance
Example

angular.module('card', [])
  .directive('card', card)

///////////////////// Логика //////////////////////////

function card($uibModal) {

  return {
    restrick: 'E',
    replace: true,
    transclude: true,
    template: 'card.html',
    controllerAs: 'card',
    controller: controller,
    bindToController: {
      type: '@'
    }
  }
}

function controller($uibModalStack, clientsService) {

  var wm = this;

  wm.close = close;

  getData();

  ///////////////////////////////////////////////

  function close() {
    $uibModalStack.dismissAll()
  }

  function getData() {
    return clientsService.card()
    .then(function(res) {
      wm.info = res.data.data;
    })
  }
}

Page 1
<card><span ng-click='card.close()'>x<span></card>
Page 2 (another method appeared
<card>
    <span ng-click='card.close()'>x<span>
    <span ng-click='card.add()'>+<span>
</card>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-18
@JIakki

We split the UI into components (by components I mean a directive, a custom element with its own template and controller), we reuse everything that can be reused.
to reduce differences and increase reusability - we pass data directly into the component through bindings, and do not request them in the controller thereof.
You can talk about any specifics only when you see at least an approximate mockup of what you want to do.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question