How to change properties of firebase array using angularfire? - angularjs

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(){});
}

Related

need to get input array value to controller

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.

ng-model is not updating the right user in AngularJS

I am kind of new in AngularJS, and I have a form that users can edit there form. But when user clicks update, it updates the form. BUT, the user does not update the user. Shown in the image
Image here
Here is my code:
<div ng-controller="MyCtrl">
<div class="people-view">
<h2 class="name">{{people.first}}</h2>
<h2 class="name">{{people.last}}</h2>
<span class="title">{{people.title}}</span>
<span class="date">{{people.date}} </span>
</div>
</div>
</div>
<!-- the form -->
<div class="list-view">
<form>
<fieldset ng-disabled="inactive">
<legend>Basic Info</legend>
<b>First Name:</b>
<input type="text" ng-model="myObject.first">
<br>
<b>Last Name:</b>
<input type="text" ng-model="myObject.last">
<button type="button" class="edit" ng-show="inactive" ng-click="inactive = !inactive">
Edit
</button>
<button type="submit" class="submit" ng-show="!inactive" ng-click="update()">Update</button>
</form>
App.js
var app = angular.module("Portal", ['ngRoute']);
app.controller('MyCtrl', function($scope) {
$scope.inactive = true;
$scope.people = {};
$scope.myObject = JSON.parse(localStorage.getItem('myObject')) || {
first : $scope.people.first,
last : $scope.people.last,
};
$scope.update = function(){
$scope.people.first = $scope.myObject.first;
$scope.people.last = $scope.myObject.last;
localStorage.setItem('myObject', JSON.stringify($scope.myObject));
};
$scope.deletePeople = deletePeople;
function deletePeople(id){
var userToDelete;
$scope.people.forEach(function(p, index){
if(p.id == id){
userToDelete = index;
}
});
$scope.people.splice(userToDelete, 1);
console.log($scope.people);
}
});
List of users
<div class="people" ng-repeat="p in people | filter:userSearch" >
<a href="#/people/{{ p.id }}">
<img ng-src="{{p.img}}"/>
<span class="name">{{p.first}} </span>
<span class="name">{{p.last}} </span>
<p class="title">{{p.title}} </p>
<span class="date">{{p.date}} </span>
</a>
</div>
The people view section should be included under <div ng-controller="MyCtrl"> div. So just move that line up so it's the first line of the code.
Your ng-model in the form needs to be people.first and people.last respectively. E.g. <b>First Name:</b>
<input type="text" ng-model="people.first">
<br>
<b>Last Name:</b>
<input type="text" ng-model="people.last">
check this
As Swailem95 said you have to move the <div ng-controller="MyCtrl"> top and it looks like below after that.
<div ng-controller="MyCtrl">
<div class="people-view">
<h2 class="name">{{people.first}}</h2>
<h2 class="name">{{people.last}}</h2>
<span class="title">{{people.title}}</span>
<span class="date">{{people.date}} </span>
</div>
<!-- the form -->
<div class="list-view">
<form>
<fieldset ng-disabled="inactive">
<legend>Basic Info</legend>
<b>First Name:</b>
<input type="text" ng-model="myObject.first">
<br>
<b>Last Name:</b>
<input type="text" ng-model="myObject.last">
</fieldset>
<button type="button" class="edit" ng-show="inactive" ng-click="inactive = !inactive">
Edit
</button>
<button type="submit" class="submit" ng-show="!inactive" ng-click="update()">Update</button>
</form>
</div>
</div>

can't get data from ng-model using signalr

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

Having issues sending scope to the template in AngularJS

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>

AngularJS $save form data in a separate hash

Using this setup I can save a form in AngularJS.
I want to have the data values pushed to the server (via the $save method) as defined in the name="" attributes.
So a form submitted like this would look like this:
Ideal Form Data
{
book: {
word : '...',
book : '...',
page : '...'
}
}
But instead its being submitted directly as a hash (without the inner book block).
Here's what my form and controller looks like:
My Form
<div>
<form data-ng-submit="save()">
<ol class="fields">
<li>
<div class="label">
<label for="word">Word: </label>
</div>
<div class="details">
<input type="text" name="word[word]" data-ng-model="word.word" />
</div>
</li>
<li>
<div class="label">
<label for="book">book: </label>
</div>
<div class="details">
<input type="text" name="word[book]" data-ng-model="word.book" />
</div>
</li>
<li>
<div class="label">
<label for="page">page: </label>
</div>
<div class="details">
<input type="text" name="word[page]" data-ng-model="word.page" />
</div>
</li>
</ol>
<nav class="actions">
<input type="submit" value="save" />
</nav>
</form>
</div>
My Controller (Angular)
var saveCtrl = function($scope, $routeParams, Word, $location) {
$scope.word = Word.get({
id : $routeParams.id
});
$scope.save = function() {
$scope.word.$save({
id : $scope.word.id
});
$location.path('/').replace();
};
}
Any ideas?
Maybe something like this would work in your controller:
Word.save({
id : $scope.word.id
}, {
book : { word: $scope.book }
});
Taken from the docs I think you need to set the postData to override the default data being posted.
non-GET "class" actions: Resource.action([parameters], postData,
[success], [error])

Resources