I am working on a post system with likes where the user can toggle the post`s like I have done everything correctly except the last step, the problem inside my v-for loop I fetch likes table from post-like relation(many to many since likes table has user_id and post_id) but it is iterating my button even when I add a condition look here-> duplicated like button, I have tried many things v-if, v-show I think the problem is with my algorithm I hope someone can fix that for me thanks.
<div class="panel panel-white post panel-shadow" v-for="post in posts" >
<div class="post-heading">
<div class="pull-left image">
<img v-bind:src="'img/profile/' + post.user.photo" class="img-circle avatar" alt="user profile image">
</div>
<div class="pull-left meta">
<div class="title h5">
<b>{{post.user.name}} </b>
made a post.
</div>
<h6 class="text-muted time">{{post.created_at | hour}}</h6>
</div>
</div>
<div class="post-description">
<p>{{post.content}}</p>
<div class="stats">
<button class="btn btn-default stat-item" #click.prevent="addLike(post.id)">
<i class="fa fa-thumbs-o-up" aria-hidden="false" style="color: blue" v-for="(like) in post.likes" v-bind:style="like.user_id===id && like.post_id===post.id?'color: blue;':'color: gray;'" > Like {{post.likes.length}}
</i> <!-- here is the duplicate problem-->
</button>
<a class="btn btn-default stat-item" #click.prevent>
<i class="fa fa-reply-all"> {{post.comments.length}}</i> Comments
</a>
</div>
</div>
<comment-input :post="post" :userId="id" :userPhoto="userPhoto"></comment-input>
<ul class="comments-list" v-for="comment in post.comments?post.comments:''">
<comments :comment="comment" :userId="id" :userPhoto="userPhoto"></comments>
</ul>
</div>
<hr>
</div>
</div>
Don't loop through the button element, try to use a method likedBythisUser to check if the current user liked the post in order to bind it to the button style :
methods:{
likedBythisUser(post,id){
return post.likes.find(like=>{
return like.user_id===id && like.post_id===post.id;
}) // return a boolean value
}
}
template :
<button class="btn btn-default stat-item" #click.prevent="addLike(post.id)">
<i class="fa fa-thumbs-o-up" aria-hidden="false" style="color: blue" bind:style="likedBythisUser(post,id)?'color: blue;':'color: gray;'" > Like {{post.likes.length}}
</i> <!-- here is the duplicate problem-->
</button>
A part of our website includes users replying to reviews posted by other users. All replies(inputs) are bound to the same ng-model. In the HTML code, there's a form that is repeated, over and over again using ng-repeat. The problem is that after a user posts a reply the form doesn't get cleared, so the next user replying to the review, is left with a box that includes the review of the previous user, and therefore has to override it.
Here's our HTML code:
<li ng-repeat="reply in review.replies">
<div class="comment-avatar">
<img src="http://i9.photobucket.com/albums/a88/creaticode/avatar_2_zps7de12f8b.jpg" alt="">
</div>
<div class="comment-box">
<div class="comment-head">
<h6 ng-if="reply.authorType == 'user'" class="comment-name">{{reply.user.name}}</h6>
<h6 ng-if="reply.authorType == 'business'" class="comment-name by-author"> {{business.name}}</h6>
<span style="margin-top: 10px">{{reply.timestamp | date: 'd MMM, yy h:mm a ' }}
<i ng-click="deleteReply(review, reply)" class="delete glyphicon glyphicon-trash"></i>
</span>
</div>
<div class="comment-content">
{{reply.reply}}
</div>
</div>
</li>
I am displaying all of the articles from the database and I've created a simple text search for it.
<input ng-model="searchText" placeholder="Search for articles" class="search-text">
And I am filtering the content from the searchText
<section class="articles" ng-repeat="contentItem in content | filter:searchText">
Now what I'm trying is to have buttons with the categories and tags from my database and to be able to filter the content based on the button values.
<span ng-repeat="category in categories">
<button ng-click="searchText = '{{category.local.name}}'" class="btn btn-primary">{{ category.local.name }}</button>
</span>
The code above is not working but for example if I'm taking out the ng-repeat and type all the categories myself it will work:
<span>
<button ng-click="searchText = 'some text'" class="btn btn-primary">Some text</button>
</span>
My question is how can be the good way to pass the content from the search input and buttons to the filter method?
Here is the full code:
<div class="container">
<h1>List of articles</h1>
<section class="search-items">
<input ng-model="searchText" placeholder="Search for articles" class="search-text">
<div class="row">
<div class="col-sm-6">
<h6>Search based on categories:</h6>
<span ng-repeat="category in categories">
<input type="button" ng-click="searchText = '{{ category.local.name }}'" value="{{ category.local.name }}" class="btn btn-primary">
</span>
</div>
<div class="col-sm-6">
<h6>Search based on tags:</h6>
<span ng-repeat="tag in tags">
<input type="button" ng-click="searchText = '{{tag.local.name}}'" value="{{ tag.local.name }}" class="btn btn-primary">
</span>
</div>
</div>
</section>
<section class="articles" ng-repeat="contentItem in content | filter:searchText">
<article class="well">
<h3>{{ contentItem.local.title }}</h3>
<p>{{ contentItem.local.content }}</p>
<span><b>Article author: {{ contentItem.local.author }}</b></span><br/>
<span class="label label-primary">{{ contentItem.local.categories.category }}</span>
<span class="label label-primary">{{ contentItem.local.tags.tag }}</span>
</article>
</section>
</div>
angular.module('myApp', [])
.controller('ThingsController', function(){
var ctrl = this;
ctrl.things = ['thing 1', 'thing 2', 'another thing']
ctrl.searchText = "something"
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ThingsController as thingCtrl">
<div ng-repeat="thing in thingCtrl.things">
<button ng-click="thingCtrl.searchText = thing">{{thing}}</button>
</div>
<pre>{{thingCtrl.searchText}}
Above snippet shows it working also using controller as syntax. Typically you don't want to use the $scope as though it's your model you want it to point to your model otherwise you may run into some weird prototypical inheritance problems. As in $scope.myModel = {someProp:4} instead of $scope.someProp = 4 using the controller as syntax also avoids this issue.
What are the nuances of scope prototypal / prototypical inheritance in AngularJS?
I have uploaded image in upload table using grids and MongoDB and I have stored image name in store table.
When I get store details first i have taken image name and check from upload table and displaying the image.
Here I am looping that image name its working but it always showing last image. I have enclosed my html and controller.
$http.get('***').success(function(data,dealers,response) {
var storelen=data.length;
console.log(storelen);
for(var i=0;i<storelen;i++) {
var storeimages=JSON.stringify(data[i].Store_Images);
var storeimages=(storeimages.replace(/\"/g, ""));
console.log(storeimages);
$http.get('***').success(function(data, status,response) {
$scope.image=data;
console.log($scope.image);
});
}
$scope.dealers = data;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ion-view >
<ion-content style="margin-top:50px;" ng-controller="ListingCtrl" data-ng-init="find()">
<div class="list card" data-ng-repeat="dealer in dealers | filter:query | filter:myFilter">
<div class="item item-thumbnail-left item-icon-right" href="#">
<img src="{{image}}" id="myimage">
<h2>{{dealer.Store_Name}}</h2>
<p>{{dealer.S_Address.city}} , {{dealer.S_Address.area}}</p>
<div class="row" style="padding:0px;">
<div class="col-90"><p style="font-size:10px">review</p></div>
<div class="col-10" style="margin-left:30px;">
<i style="right:0px;font-size:18px;color:#ff6f00;"class="icon ion-ios-location subdued"></i>
</div>
</div>
<div class="row" style="padding:0px;">
<div class="col" style="padding: 0px;"><p style="font-size:14px;" listing-rating rating-value="rating" max="5"></div>
</div>
</div>
<div class="item tabs tabs-secondary tabs-icon-left">
<a class="tab-item" ng-href="tel:{{dealer.S_Phone.number}}">
<i class="icon ion-ios-telephone " ></i>
Contact Now
</a>
<a class="tab-item" style="color:green;">
<i class="icon ion-pricetags"></i>
Email
</a>
<a class="tab-item" style="color:#ff6f00;" >
<i class="icon ion-wand" ></i>
Check Now
</a>
</div>
</div>
</ion-content>
</ion-view >
In my table having 3 images, I am getting 3 images in console, but html I am always getting the last image.
I want to display image with in ng-repeat is there any other way doing it ?
My app has a web service that returns the following json array
[{"id":1,"title":"Jeans #1","description":"Jeans Description Of Jeans #1","gender":1,"waists":[{"id":1,"sizeString":"28","enabled":true,"gender":1},{"id":3,"sizeString":"30","enabled":true,"gender":1}],"inseams":[{"id":1,"sizeString":"28","enabled":true,"gender":1},{"id":2,"sizeString":"29","enabled":true,"gender":1}],"style":{"id":1,"styleName":"Style #1","enabled":true,"gender":1},"ctr":0,"colorAndPatterns":[{"id":2,"description":"Green","background":"#00FF00","enabled":true,"gender":1},{"id":1,"description":"Red","background":"#FF0000","enabled":true,"gender":1}],"productOverviews":[{"id":3,"description":"Straight leg","enabled":true,"gender":1},{"id":1,"description":"Slim through thigh","enabled":true,"gender":1}],"enabled":true,"firstJeansImagesBycolor":[{"id":2,"colorAndPatternId":2,"enabled":true},{"id":6,"colorAndPatternId":1,"enabled":true}],"allJeansImagesByColor":[{"id":1,"colorAndPatternId":2,"enabled":true},{"id":2,"colorAndPatternId":2,"enabled":true},{"id":3,"colorAndPatternId":2,"enabled":true},{"id":4,"colorAndPatternId":2,"enabled":true},{"id":5,"colorAndPatternId":1,"enabled":true},{"id":6,"colorAndPatternId":1,"enabled":true},{"id":7,"colorAndPatternId":1,"enabled":true},{"id":8,"colorAndPatternId":1,"enabled":true}]}]
I am using angular's ng-repeat directive to iterate through the json array in the following manner
<div ng-repeat="jean in currentJeans">
<div class="col-xs-4 jeans-listing at-repeat-twirl-in at-repeat-twirl-out" ng-repeat="colors in jean.colorAndPatterns | filter:{id:currentColorId}">
<a href="#/jeans-details/{{jean.id}}" class="thumbnail">
<img ng-src="static/images/jeans/{{jean.id}}/{{colors.id}}/{{jean.firstJeansImagesBycolor[$index].id}}.jpg" alt="Jeans Image"/>
<div class="caption">
<h5 class="text-center">{{jean.title}}</h5>
</div>
<button class="btn btn-default btn-sm" style="position: absolute;top:100px;left:30%;display:none;color:white;background-color:rgba(0,0,0,0.4)"><i class="fa fa-search"></i> Quick View</button>
</a>
</div>
</div>
The problem i 'm facing is this expression evaluates to nothing
{{jean.firstJeansImagesBycolor[$index].id}}
Is this the right way to access arrays inside json objects or not what i 'm doing wrong ??
Here is the link to JSFiddle
It is working perfectly fine. I just printed the path given and it prints:
http://jsfiddle.net/pfgkna8k/3/
<div ng-app="lists">
<div ng-controller="ProductList">
<div ng-repeat="jean in currentJeans">
<div class="col-xs-4 jeans-listing at-repeat-twirl-in at-repeat-twirl-out" ng-repeat="colors in jean.colorAndPatterns | filter:{id:currentColorId}">
<a href="#/jeans-details/{{jean.id}}" class="thumbnail">
<img ng-src="static/images/jeans/{{jean.id}}/{{colors.id}}/{{jean.firstJeansImagesBycolor[$index].id}}.jpg" alt="Jeans Image"/>
static/images/jeans/{{jean.id}}/{{colors.id}}/{{jean.firstJeansImagesBycolor[$index].id}}.jpg
<div class="caption">
<h5 class="text-center">{{jean.title}}</h5>
</div>
<button class="btn btn-default btn-sm" style="position: absolute;top:100px;left:30%;display:none;color:white;background-color:rgba(0,0,0,0.4)"><i class="fa fa-search"></i> Quick View</button>
</a>
</div>
</div>
</div>
</div>