Changes in $http data from 1 controller to affect another in AngularJS? -


So if my data is coming from json $ http, how can I do it if I'm a controller I vote on it, will it be sorted by the votes on any other controller?

I have not yet been working correctly and it seems that the shared data catches any change between the controller ... look at it

  App.controller ('Vote Controller', Function ($ Scope, Music Data) {musicData.getMusic (.) Then (Function (Data) {$ scope.music = data.data;}); $ Scope UpVote = Function ) {Music.likes ++;}; $ scope.downVote = function (music) {music.dislikes--;};}); // Controller app.controller for top 5 chart ('topfivecontroller', function ($ radius, music data) {musicData.getMusic (). Then (function (data) {$ scope.music = data.data;}); $ Scope .sortorder = '-likes';}); // application.factory ('musicData', function ($ http) {var factory = {}; factory-gatemusic =) function {$ http.get ('app / data / music) to share data between factory controllers .json) ');}; Return factory;});   

And this is what I have in markup

  & lt; Div id = "topmusic" ng-controller = "topfivecontroller" ng-model = "sortorder" & gt; & Lt; H3 class = "title" & gt; & Lt; I class = "fa-fa-hand-o-right" & gt; & Lt; / I & gt; Top 5 titles & lt; / H3 & gt; & Lt; Ul & gt; & Lt; Li ng-repeat = "Music in music: Music By Order: Sort | Limit: 5" & gt; {{Music.artist}} - "{{music.title}}" & lt; / li & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt; Div id = "voteMusic" ng-controller = "vote controller" & gt; & Lt; H1 & gt; Vote here & lt; / H1> & Lt; Ul & gt; & Lt; Li class = "likeMe" ng-click = "upVote (music)" & gt; & Lt; I class = "fa-fa-rings-up" & gt; & Lt; / I & gt; E.g. {{music.likes}} & lt; / li & gt; & Lt; Li class = "dislikeMe" ng-click = "down brit (music)" & gt; & Lt; I class = "fa-fa-thums-down" & gt; & Lt; / I & gt; Dislikes - {{music.dislikes}} & lt; / li & gt; & Lt; / Ul & gt; & Lt; / Div & gt;   

How does the json file look like this

  {"music": [{"artist": "artist 1", "title": " "Title", "likes": 5, "dislikes": 1}, "title", "likes", 10, "dislikes": 1}, {"artist": "artist 2", "title": "title "Artist": "Artist 3", "Title": "Title 3", "Likes": 3, "Dislikes": 1}, {"Artist": "Artist 4", "Title": "Title 4 "" "Like": 2, "Dislike": 1}, {"Artist": "Artist 5", "Title": "Title 5", "Likes": 1, "Dislikes": 1}]}    

With its apprehension, you In fact, you will need to use the $ root scope. $ Transmit $ instead of scope $ emit, as sma suggests, as your markup, the controller is not actually the child of topFiveController in your controller, you have $ rootScope Will need to be injected as dependency, and then use it:

  $ rootScope. $ Broadcast ('vote');   

and In topFiveController:

  $ scope. $ ('Vote', function () {// sort});   

If you want to update the music object to the topfile controller, then do not recover the new vote from the music service, you can also do it in your Vote Controller: $ rootscope. $ Broadcast ('vote', $ scope music);

Then do your topFiveController in:

  $ scope. At $ ('vote', function (music) {$ scope.music = music; // sort});    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -