here is my .html :
<ul class="messages list-unstyled" ng-repeat="newMessage in messages">
<li>
<p>
<span class="username">{{newMessage.username}}</span><br />
</p>
<p>
<span> {{newMessage.message}}</span>
</p>
<p class="no-pad-bottom date-posted">Send {{ newMessage.date }} <span /></p>
<div class="comments">
<ul class="list-unstyled" ng-repeat="newComment in comments">
<li>
<p>
<span class="commentor"> {{newComment.username}}</span>
<span> {{newComment.message}}</span>
</p>
<p class="no-pad-bottom date-posted">Send {{ newComment.date }} <span /></p>
</li>
</ul>
<form class="add-comment">
<div class="row">
<div class="col-md-10">
<input type="text" class="form-control" ng-model="myComment" placeholder="Add a comment" />
</div>
<div class="col-md-2">
<button class="btn btn-default btn-sm" type="submit" ng-click="addComment(newMessage.id)">Comment</button>
</div>
</div>
</form>
</div>
</li>
</ul>
everything is working. i can send my messages, and i can even send posts, but i get null from ng-model="myComment"
here is the method in js controller
$scope.addComment = function (messageId) {
myChatHub.server.addComment(messageId, userName, $scope.myComment);
};
can you please tell me what's wrong?
You are breaking the golden rule of always having a dot in ng-model .. or in other words using an object
ng-repeat creates a child scope and your primitive assigned to ng-model therefore only exists in the child scope since primitives have no inheritance.
Create model object in controller
$scope.model={}
And then use that ng-model="model.myComment" and change your post to:
$scope.addComment = function (messageId) {
myChatHub.server.addComment(messageId, userName, $scope.model.myComment);
};
This 3 minute video will be very enlightening
Related
I need to get array values from form to controller and could not find a way to do so.
HTML Form:
<div class="input-wrap">
<label>CNC Location</label>
<div class="input-inner">
<div class="input-area input-row">
<input type="text" id="latitude" name="locationLat" ng-model="actorData.cncLocation['lat']" placeholder="Latitude">
<input type="text" id="longitude" name="locationLong" ng-model="actorData.cncLocation['long']" placeholder="Longitude">
<input type="text" id="location-name" name="locationName" ng-model="actorData.cncLocation['name']" placeholder="Name">
</div>
<div class="buttons-area">
<a href class="remove-row"><i class="fa fa-minus-square"></i></a>
<a href class="add-row" ng-click="addRow()"><i class="fa fa-plus-square"></i></a>
</div>
</div>
<div ng-messages="actorForm.cncLocation.$dirty && actorForm.cncLocation.$error">
<span class="error-msg" ng-message="required">Required</span>
</div>
</div>
Controller:
angular.forEach($scope.actorData.cncLocation, function(){
$scope.cncLocation.push($scope.actorData.cncLocation);
console.log($scope.cncLocation);
});
I need to send cncLocation array with objects which includes {"lat": "", "long":"", "name":""} where user can add and remove rows.
I'm trying to get the number of comments a post has, but can't find a way to do it. I have a plunk if you want to check it.
<div class="anuncios">
<div class="panel panel-default" ng-repeat="posts in post | orderBy:'-FechaAnuncio'">
<div class="panel-heading" style="overflow:hidden;">
<p>{{posts.NombreLocal}}</p>
</div>
<div class="panel-body">
<p>{{posts.Anuncio}}</p>
<hr>
<small>{{posts.FechaAnuncio | date:'HH:mm:ss dd-MM-yyyy'}}</small>
<button class="btn btn-primary" style="float:right;" ng-click="showme = !showme"><span ng-hide="showme">Show Comments: {{posts.comments.length}}</span><span ng-show="showme">Hide Comments</span></button>
<div>
<div class="well" ng-show="showme">
<div class="input-group">
<input type="text" class="form-control" placeholder="Comment Here..." ng-model="comment.text">
<span class="input-group-btn">
<button class="btn btn-default" type="button" ng-click="addComment(posts, comment)">Publish!</button>
</span>
</div>
<!-- /input-group -->
<div class="cmt">
<div class="panel" ng-repeat="comments in posts.comments">
<p>{{comments.NombreLocal}}</p>
<p>{{comments.FechaComentario | date:'HH:mm:ss dd-MM-yyyy'}}</p>
<hr>
<p>{{comments.Comment}}</p>
<p></p>
</div>
<!--FIN DE COMENTARIOS -->
</div>
</div>
</div>
</div>
</div>
</div>
http://plnkr.co/edit/AIx0x7?p=preview
The number of comments should display next to "Show Comments:"
This is my Firebase Structure:
Firebase structure image
Since comments is an object, the easiest thing to do is to count the number of keys it has. You'll need to add this to your controller, or you could create your own filter.
app.controller('AnunciosCtrl', function($scope, $firebaseArray, $timeout) {
...
$scope.sizeOf = function(obj) {
return Object.keys(obj || {}).length;
};
});
HTML:
<span ng-hide="showme">Show Comments: {{ sizeOf(posts.comments) }}</span>
Plunker
In your controller do:
$scope.numComments = $scope.post.comments.length;
In your html :
...>Show Comments: {{numComments}}<...
I get the correct record from the firebase array using the $getRecord but I can not save the changes made in the input fields. Our Add Controller works. What have we done wrong? Can anyone help? Thank you!
Edit Controller
app.controller("EditRecipeController", function($scope, $location, $routeParams, $firebaseArray) {
$scope.recipes.$loaded().then(function(recipeid) {
$scope.recipe = recipeid.$getRecord($routeParams.id);
$scope.editRecipe = function(recipe) {
$scope.recipes.$save(recipe).then(function() {
})};});});
Add Controller
app.controller("AddRecipeController", ["$scope", "createRecipes", function($scope, createRecipes) {
$scope.recipes = createRecipes;
$scope.addRecipe = function() {
$scope.recipes.$add({
title: $scope.title,
lead: $scope.lead,
keyword: $scope.keyword,
ingredient: $scope.ingredient,
instructions: $scope.instructions
});
alert("Your recipe has been succefully saved!")
// reset the recipe input
$scope.recipe = "";
};}]);
Factory
app.factory("createRecipes", ["$firebaseArray",function($firebaseArray) {
var ref = new Firebase("https://fiery-inferno-8595.firebaseio.com/recipes/");
return $firebaseArray(ref);}]);
HTML
<section id="{{recipe.$id}}" class="recipe-description editor">
<form ng-submit="addRecipe()">
<section class="headColumn">
<div class="mySearch">
<span>My search: </span>
<span ng-bind="search.$"></span>
</div>
<h2>
<input type="text" ng-model="title" name="recipeTitle" ng-value="recipe.title" required="">-
<input type="text" ng-model="lead" name="recipeLead" ng-value="recipe.lead" required="">
</h2>
<ul class="keywords">
<li><label></label>Categories: </li>
<li ng-repeat="keyword in keywords track by $index">
<input type="text" ng-model="keywords[$index]" name="recipeKeyword" ng-value="recipe.keywords" placeholder="Add a keyword">
<button class="remove-field" ng-show="$last" ng-click="removeKeyword()">-</button>
</li>
<button class="add-field" ng-click="addKeyword()">+</button>
</ul>
</section>
<section class="sideColumn">
<h4>Ingredients</h4>
<ul class="ingredients">
<li ng-repeat="ingredient in ingredients track by $index">
<input type="text" ng-model="ingredients[$index]" name="recipeIngredient" ng-value="recipe.ingredients" placeholder="Add an ingredient">
<button class="remove-field" ng-show="$last" ng-click="removeIngredient()">-</button>
</li>
<button class="add-field" ng-click="addIngredient()">+</button>
</ul>
</section>
<section class="mainColumn">
<div class="readytime">
<h4>Time until ready</h4>
<ul>
<li>
<span>Preperation Time: </span>
<input type="text" ng-model="preptime" name="recipePreptime" ng-value="recipe.preptime" placeholder="Add preperation Time">
</li>
<li>
<span>Cooking Time: </span>
<input type="text" ng-model="cookingtime" name="recipeCookingtime" ng-value="recipe.cookingtime" placeholder="Add cooking Time">
</li>
</ul>
</div>
<div class="instructions">
<h4>Instructions</h4>
<!--TO DO: This input should work like textarea -->
<input type="text" ng-model="instructions" name="recipeInstructions" ng-value="recipe.instructions">
</div>
</section>
<button ng-click="addRecipe()" ng-hide="recipe" type="submit">Save the Recipe</button>
<button ng-click="editRecipe()" type="submit" ng-show="recipe">Update the Recipe</button>
</form>
You're edit function:
$scope.editRecipe = function(recipe){
$scope.recipes.$save(recipe).then(function(){});
}
is expecting the "recipe" to be passed in, but I don't see it being passed in anywhere. It looks like you are saving the current recipe to:
$scope.recipe
so if $scope.recipe has the correct object, I think you just have to save the scoped recipe opposed to passing it in.
$scope.editRecipe = function(){
$scope.recipes.$save($scope.recipe).then(function(){});
}
I have a list of users where by if the user clicks on a "edit" icon next to each person name, they have options appear.
I'm wanting to make it so that the user can only edit one person at a time and not edit lets say, two or more people at a time.
Example: If the user selects edit icon, open edit options. If user selects another persons edit icon, close current icon and show the users next selected edit options.
I'm using to toggle the views:
ng-click="editDetails = ! editDetails"
ng-class="{ 'unseen': ! editDetails }
Here is my code:
<div class="col-xs-12 col-md-6 col-md-push-6">
<div ng-show="memberNeeded" class="message">Please add hunt group members</div>
<h4>Hunt group call order</h4>
<div class="list-box">
<ul id="finalList" ng-class="{disable: details.disabled}">
<li ng-repeat="item in list" switch data-id="{{item.id}}" data-active="{{item.isActive}}" class="clearfix {{control}}">
<strong>{{item.firstName}} {{item.lastName}}</strong>
<i class="vf-i vf-i-pen pull-right" ng-click="editDetails = ! editDetails"></i>
<div class="list-panel clearfix unseen" ng-class="{ 'unseen': ! editDetails }">
<p class="pull-left">
Active
</p>
<div class="pull-left">
<div class="toggle-bg switch-toggle {{control}}">
<label class="{{control}}">{{control}}</label>
<input type="radio" name="toggle" value="off" ng-model="control" ng-click="updateUser($index, 'off')">
<input type="radio" name="toggle" value="on" ng-model="control" ng-click="updateUser($index, 'on')">
<span class="switch {{control}}"></span>
</div>
</div>
<button class="btn btn-del pull-right">Remove</button>
</div>
</li>
</ul>
</div>
</div>
If you only want one editor active at a time, just store the active item rather than a flag. On ng-click set the activeItem. Example below:
In your controller:
$scope.editor = {
activeEditItem: null
};
and your markup becomes:
<div class="col-xs-12 col-md-6 col-md-push-6">
<div ng-show="memberNeeded" class="message">Please add hunt group members</div>
<h4>Hunt group call order</h4>
<div class="list-box">
<ul id="finalList" ng-class="{disable: details.disabled}">
<li ng-repeat="item in list" switch data-id="{{item.id}}" data-active="{{item.isActive}}" class="clearfix {{control}}">
<strong>{{item.firstName}} {{item.lastName}}</strong>
<i class="vf-i vf-i-pen pull-right" ng-click="editor.activeEditItem === item ? editor.activeEditItem = null : editor.activeEditItem = item"></i>
<div class="list-panel clearfix unseen" ng-class="{ 'unseen': editor.activeEditItem !== item }">
<p class="pull-left">
Active
</p>
<div class="pull-left">
<div class="toggle-bg switch-toggle {{control}}">
<label class="{{control}}">{{control}}</label>
<input type="radio" name="toggle" value="off" ng-model="control" ng-click="updateUser($index, 'off')">
<input type="radio" name="toggle" value="on" ng-model="control" ng-click="updateUser($index, 'on')">
<span class="switch {{control}}"></span>
</div>
</div>
<button class="btn btn-del pull-right">Remove</button>
</div>
</li>
</ul>
</div>
</div>
It won't work as it will toggle all your classes.
What I would do if I were you:
In your "item" object add a variable "editActive" set it by default to false
Change your ng-class="..." by ng-show="item.editActive"
In your controller create a variable $scope.currentActive
Create a function in your controller that will be triggered by the ng-click which will get the currentActive, if there is one then set its editActive value to false. Then set the new currentActive value and set its editActive to true
It should work I think.
MY issue is this:
When I request the data from the server this sent the data correctly but in LoginController after doing the validation I'm trying to populate the user's username and email but those variables are not being printed in the template. However if I just send those variables in a simple JSON such as $scope.details = [{ 'username':'Karman', 'username':'karman#mail.com'}]; it's working fine, what am I doing wrong? Is there some issue with ng-repeat directive?
Thanks in advance to whom can help me out with this issue.....
ps: I'm using sillex and ng-resource just in case
<body ng-controller="LoginController">
<div class="col-xs-3 details-user">
<div class="col-xs-1">
<img src="web/img/avatar.jpg" alt="..." class="img-circle">
</div>
<div class="col-xs-2">
<ul>
<li ng-repeat="detail in details">
{{detail.username}} -- {{detail.email}}
</li>
</ul>
</div>
</div>
Controller:
function LoginController($scope, Login) {
var currentResource;
$scope.login = function () {
Login.query({email: $scope.email}, function success(data){
$scope.details = data;
//$scope.details = [{ 'username':'Karman', 'username':'karman#mail.com'}];
});
}
}
Form:
<!-- Main DIV -->
<div id="login" class="login-main">
<div class="form-wrap">
<div class="form-header">
<i class="fa fa-user"></i>
</div>
<div class="form-main">
<form>
<div class="form-group">
<input ng-model="email" type="text" class="input-large" placeholder="Tu email aqui..." required>
<input ng-model="password" type="password" class="input-large" placeholder="Tu password aqui..." required>
</div>
<button ng-click="login()" ng-show='addMode' class="btn btn-success">Sign In</button>
</form><!-- end form -->
</div><!-- end div form-main -->
<div class="form-footer">
<div class="row">
<div class="col-xs-7">
<i class="fa fa-unlock-alt"></i>
Forgot password?
</div>
<div class="col-xs-5">
<i class="fa fa-check"></i>
Sign Up
</div>
</div>
</div>
</div>
</div>