How to reuse controller for different views in AngularJS? -
I get object data from a JSON request, such as:
{"class =" text " Slide ": [[" Image ":" http://lorempizza.com/380/240 "," Title ":" Slide A "," Details ":" I give you a description "}, {" image ": "Http://lorempizza.com/380/240", "title": "slide bbi", "description": "i give you details"}], "title": "interesting object", "description": "Ready to advertise." } I want to use two different views for the title of another object and the second for the details, everything on the same page . Why? Because I am using UI ideas to display them. index.html & lt; Div ui-view = "slider" & gt; & Lt; / Div & gt; & Lt ;! - This slider is - & gt; & Lt; Main ui-visual role = "main" & gt; & Lt; / Main & gt; & Lt ;! - Title and descend here - & gt; The problem is here:
slider.html & lt; Div ng-controller = "InterestingObjectCtrl" & gt; & Lt ;! - Display Slider - & gt; & Lt; / Div & gt; main.html & lt; Div ng-controller = "interestObjectCtrl" & gt; & Lt ;! - Display title and description - & gt; & Lt; / Div & gt; Interesting Object CTRL.js Greeting service. Gate () .service (data) {$ timeout (function () {$ Scope. $ Apply (function () ($ scope.interestingObject = data;});})}}) Interesting object CTRL will load the same JSON as an unsavory HTTP request. The correct way to solve it or "angular way" will be ?
Setting a flag (such as if ($ Scope.requestAlreadyMade) return; ) will not definitely work. HTTP request Can not be a big deal, but this problem Or the point of the call.
Calling the controller as a cover, like the following example, will be loaded with every page on the website, which is the worst. / P> & lt; div ng-controller = "interesting object Ctrl"> gt; div ui-view = "slider" & gt; & gt; / div & gt; & lt; ! - The slider is -> main ui-visible role = "main" & gt; & lt; / main & gt; & lt ;! - title and desc -> gt; & gt; & lt; / div & Gt;
& lt; div ng-controller = "MainCtrl" & gt; & Lt; Div ui-view = "slider" & gt; & Lt; / Div & gt; & Lt ;! - This slider is - & gt; & Lt; Main ui-visual role = "main" & gt; & Lt; / Main & gt; & Lt ;! - Title and descend here - & gt; & Lt; / Div & gt; Once the data is loaded in MainCtrl and it prevents child controllers:
Gourmet service. Gate () .sevate (function) {$ scope. $ Broadcast ('datloged', data);}); Interesting object CTRL.js
$ scope $ ('Datedoaded', function (data) {// what you want to do});
Comments
Post a Comment