I'm writing an app that uses the same table with the same data in multiple places. I created a custom directive that allows me to reuse this table. Unfortunately, if I edit the table in one instance, the other instance does not refresh. How do I link these two so that any edits I make to one show up in the other?
-
How are you passing the table data to the directive?James– James2015-03-02 17:44:28 +00:00Commented Mar 2, 2015 at 17:44
-
I have a service that makes an AJAX call to my Django REST backend. The app is a media management app that has Image and Video tabs, as well as a slideshow tab. The Image and Video tabs have their own tables that are duplicated in the Slideshow tab since users can create slideshows with existing media. I think the problem is that the Slideshow instances of these tables are nested in the slideshow controller. If I make a test tab that uses the directive, it stays in sync with the main image tab, but the Slideshow instance doesn't.Jacob– Jacob2015-03-02 17:53:48 +00:00Commented Mar 2, 2015 at 17:53
1 Answer
It sounds like you've mostly figured it out, the hard part is getting your data into a shape where the videos and photos can be shared by the slide show. I recommend doing this in a shared data access object returned by a separate factory in Angular, rather than directly in a scope. I've got a sample in Plunkr if it helps.
The sample has a directives that binds to shared data, retrieved from a factory as an object injected into two separate scopes. In your case, you would have to add methods to retrieve data from the server, and shape it for display.
testApp.factory("News", [function () {
var news = {
"stories": [
{"date": new Date("2015-03-01"), "title": "Stuff happened"},
{"date": new Date("2015-02-28"), "title": "Bad weather coming"},
{"date": new Date("2015-02-27"), "title": "Dog bites man"}
],
"addStory": function (title) {
var story = {
"date": new Date(),
"title": title
};
news.stories.push(story);
}
};
return news;
}]);
Both controllers reference the same factory for the data:
testApp.controller("FirstController",
["$scope", "News", function ($scope, news) {
$scope.news = news;
}]);
testApp.controller("SecondController",
["$scope", "News", function ($scope, news) {
$scope.news = news;
}]);
Views then pass the data into to the news list directive, which both shares the data and keeps the directive relatively dumb.
<div ng-controller="FirstController">
<news-list news="news" title="'First List'"></news-list>
</div>
<div ng-controller="SecondController">
<news-list news="news" title="'Second List'"></news-list>
</div>
The news-list directive is just dumb formatting in this example:
testApp.directive("newsList",
function() {
var directive = {
"restrict": "E",
"replace": false,
"templateUrl": "news-list.html",
"scope": {
"news": "=news",
"title": "=title"
}
};
return directive;
});
View template:
<div class="news-list">
<p>{{title}}</p>
<ul>
<li ng-repeat="story in news.stories | orderBy:'date':true">{{story.date | date:'short'}}: {{story.title}}</li>
</ul>
<form>
<input type="text" id="newTitle" ng-model="newTitle" />
<button ng-click="news.addStory(newTitle)">Add</button>
</form>
</div>
6 Comments
imageList, but just used the imageList set by the parent scope? That way you can be sure it is the same.imageList in the SlideshowController be an equivalent action? I've tried only setting it in the ImageController, but it hasn't helped. Though they're both getting their data from the Images service, they just don't update if an edit is made to either. It seems like the initial get call in each is creating two separate but identical datasets. I don't understand why this happens when one instance is nested, but works correctly when they're both at the same level since SlideshowController doesn't touch the data.