Angularjs example MotoAds more advanced directive
Another thing we will add to MotoAds demo application is a feature which allows us to comment each advert. A good idea would be to realize this as AngularJS directive. It will look like in the picture below. We can see all added comment to advert, click on Comment link activate the comment form with Preview, Send and Cancel buttons.
The comment form we will realize as AngularJS directive. Before we start let's look at changed
Step 1
We create
Step 2
We in
Step 3
We add simple
Step 4
We add simple
Step 5
We
If you want to run this example on your computer, you can download sources from GitHub.
Any comment would be highly appreciated.
adverts.json
, there is the additional comments
field (array of comments):
[ { "brandName": "Audi", "modelName": "A1", "year": 2011, "price": 35000, "imageUrl": "img/audi_a1_1.jpg", "countryName": "Germany", "regionName": "Bavaria", "comments" : [ "This car is awesome I want it\nBeatiful color\nFunny look", "Very cool vehicle\nJust perfect" ] } ] ]]>
Step 1
We create
commenForm.html
template which we use in directive:
Step 2
We in
directives.js
we create AngularJS directive:
motoAdsApp.directive('commentForm', function(Advert) { return { restrict: "E", replace: true, scope: { commentModeOn: '=', advertId: '=' }, templateUrl: "views/commentForm.html", link: function(scope, element, attrs) { scope.content = ''; scope.previewModeOn = false; scope.closeCommentForm = function() { scope.previewModeOn = false; scope.commentModeOn = false; }; scope.isUnchanged = function() { return angular.isUndefined(scope.content) || angular.equals(scope.content, ''); }; scope.togglePreviewMode = function() { scope.preview = scope.content; scope.previewModeOn = !scope.previewModeOn; }; scope.sendCommentForm = function() { var advert = Advert.get({advertId: scope.advertId}, function() { advert.comments.push(scope.content); Advert.update(advert, function() { scope.closeCommentForm(); scope.$parent.advert.comments.push(scope.content); scope.content = ''; }); }); scope.preview = scope.content; scope.previewModeOn = true; }; } }; }); ]]>
Step 3
We add simple
CommentController
into controllers.js
:
motoAdsApp.controller('CommentController', ['$scope', function($scope) { $scope.commentModeOn = false; $scope.isAnyComment = function() { return ($scope.advert.comments.length > 0); }; }]);
Step 4
We add simple
newlines
filter into filters.js
:
motoAdsApp.filter('newlines', function() { return function(text) { if (text) { return text.replace(/\n/g, '<br/>'); } return text; }; });
Step 5
We
comment-form
directive into adverts.html
and some additional code to list comments and activate the comment form:
Image/Country/Region | Brand | Model | Year | Price ($) | Operation |
---|---|---|---|---|---|
{{advert.countryName}}, {{advert.regionName}} |
{{advert.brandName}} | {{advert.modelName}} | {{advert.year}} | {{advert.price}} |
Edit
Remove Comment |
Comment(s):
|
|||||
|
Any comment would be highly appreciated.
Komentarze