javascript - Angularjs share methods between controller -
i have application displays news feed in 1 page (home page) , in displays feed user alone (user profile page).
both pages , behave in same way. change in content due different urls called.
how solve in angularjs?
i have home controller has methods/logic used display home page.
now need have 1 more controller calls other url , same thing home controller.
i thinking of parent controller , 2 controllers. looks in angularjs controller inheritance determined dom structure. in case 2 different pages. cant create dom style inheritance.
i confused on how solve without duplicating code in both controllers.
i happy add more details question if needed. went through number of google results couldn't figure out how this.
a service (not controller) should own domain model, , provide model api rest of application (usually controllers) use.
i suggest @ least 2 services, 1 newsfeed, , 1 profile, , maybe 1 more shared functions (these functions can passed collection, or individual post, argument). inject appropriate service(s) each controller.
the "look" should in html template pull in using ng-view or ng-include. template's ng-repeat refer collection $scope property reference data stored in 1 of services.
for example of newsfeed service, , how controller can reference data, see should services expose asynchronicity?
i wouldn't worry repeating code needed use $http service , return promise in each service. someday profile page may start look/behave differently newsfeed page.
Comments
Post a Comment