angularjs - Refactor similar controllers in different modules -


My application depends on many fingers module, but each module has to deal with the same data. The module is used in various places on my page.

I have created a set of controllers, who are basically responsible for handing over data to the $ scope variable. I think there are a lot of "code odor" and I have bad feelings with all duplicate codes. But I do not know how to stop it now.

Features Documentation and Infomatitier My $ routeProvider .

What is a better way to deal with such cases?

Controller 1
  Angular Modules ('MyApp.selectorDocumentations'). Controller ( 'selector documents' CTRL', function ($ radius, FacetService, properties, documentation) {$ scope.documentationsData = documentations.data; $ scope.documentations = documentations.data.hitlist.content; $ scope.currentPage = documentations. data Khitlistkpejknanbr; $ Scopktotlpges = Dokyumenttionskdetakhitlistkpejktotlkpejej; $ Scopkhitlistcontent = Dokyumenttionskdetakhitlistkkantent; $ Scopktotlelements = Dokyumenttionskdetakhitlistkpej .totalElements; FacetService.prepare (documentations.data); $ scope.hardReset = function () {FacetServicekhardReset (wrong, documentations.data);}; $ scope.manufacturer = function (item) {return Attributes.getManufacture R ( Item);}; $ Scope.project = function (item) {return attributes.getProject (item);}; $ Scope.projectBookVersion = F Kshn (item) {return AttributeskgetProjectBookVersion (item);};});   

Controller 2
  Angular Modules ('MyApp.selectorInfomaterial'). Controller ( 'SelectorInfomaterialCtrl', function ($ scope, FacetService, properties, infomaterial) {$ Scope.informaterialData = infomaterial.data; $ scope.infomaterials = infomaterial.data.hitlist.content; $ scope.current p = infomaterial.data. Hitlistkpejknanbr; $ Scopktotlpges = Infomterialkdetakhitlist. Pejktotlpges; $ Scopkhitlistcontent = Infomterialkdetakhitlistkkantent; $ Scopktotlelements = Infomterialkdetakhitlistkpejktotlelements; Fastsrvis .prepare (infomaterial.data); $ scope.hardReset = function () {FacetService.hardReset (wrong, infomaterial.data);}; $ scope.mediaType = function (item) {return attributes .getMediaType (item);}; $ Scope.mediaTitle = function (item) {return attributes.getMediaTitle (item);}; $ Scope.mediaSubline = function (item) {return attributes.g Satemediaabline (item);};});    

Wow, these controllers are basically the same. I will take all that code into a factory, and it will start with documentation or infomaterial.

  app.controller ('Selector Document' Ctrl, Function ($ Scope, Documentation, MyadhaHandler) {$ scope. MyDataHandler = New MyDataHandler (documentations);}); App.controller ('SelectorInfomaterialCtrl', function ($ scope, infomaterial, MyDataHandler) {$ scope.myDataHandler = New MyDataHandler (informaterial);});   

They can be started in any state in the ui-router . If you need assistance in building a factory / service, please let me know.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -