$scope.Form.field.$dirty = true is not working - html

I want to set dirty flag for edited cells in the table. When I click the save button I need to check the edited field is dirty is true or not. Because X-editable is updating entire table cell values both edited and unedited cell values.I need to check what are all the edited fields using dirty flag and if that field is dirty then that fields only i have to save in to mongodb.
for that I used this line to set the $dirty:
$scope.Form.username.$dirty = true; // this throwing TypeError:$dirty is undefined error
$scope.Form.$dirty = true;
//it is working
html code:
<form name="profileform">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" id="myModal">
<!-- Modal content-->
<div class="modal-content" style="margin-top:135px">
<div class="modal-header">
<h4 class="modal-title pull-left"> Add New Role</h4>
<button type="button" class="close pull-right"
data-dismiss="modal" aria-hidden="true">
x
</button>
</div>
<div class="modal-body">
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong>Role<span style="color:green;">*</span></strong></h6>
</div>
<div class="col-xs-9">
<input type="text" name="Name" class="form-control" ng-model="Role.RoleName" />
<!--<span class="error" ng-show="profileform.RoleName.$invalid ">Please enter a Role Name</span>-->
</div>
</div>
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong> Description</strong></h6>
</div>
<div class="col-xs-9">
<textarea name="Description" style="width: 100%; max-height: 200px; max-width: 100%;"
ng-model="Role.Description" maxlength="255"></textarea>
</div>
</div>
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong>IsActive?</strong></h6>
</div>
<div class="col-xs-9">
<input type="checkbox" name="IsActive" class="form-control" ng-model="Role.IsActive" style="width:20px;" />
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="AddRole()" ng-disabled="profileform.$invalid" data-dismiss="modal">Save</button>
<button class="btn btn-primary" data-dismiss="modal" ng-click="deselect()">Cancel</button>
</div>
</div>
</div>
</div>
</form>
controller code:
$scope.AddRole = function () {
debugger;
console.log($scope.profileform.RoleName.$dirty);
console.log($scope.profileform.Description.$dirty);
$http.post('/AddNewRole', $scope.Role).then(function (response) {
//console.log(response);
$notify.success('Success', 'record inserted Successfully');
refresh();
});
};

Because $dirty is not defined in username object,or username is itself not defined, To fix the issue you need to first define the username into the Form object also please make sue Form is also into the $scope.
$scope.Form.username = {};
Then you can add $dirty into the username object like below
$scope.Form.username['$dirty'] = true

$dirty : It will be TRUE, if the user has already interacted with the form or if the field has been modified.
Try this :
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function MyCtrl($scope) {
$scope.FormSubmit = function () {
console.log($scope.form.username.$dirty);
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<form name="form" novalidate class="simple-form">Name:
<input type="text" name="username" ng-model="user.name" required />
<br />
<input type="submit" ng-click="FormSubmit()" value="Save" />
</form>
</div>

Try with names not ng-model.
angular.module('exApp',[]).controller('ctrl', function($scope){
$scope.name="Mani"; $scope.emails = "mani#gmail.com";
$scope.sub = function(){
console.log($scope.frm.yourname.$dirty, $scope.frm.mail.$dirty);
$scope.frm.yourname.$dirty = true;
console.log("I was set '#true': " + $scope.frm.yourname.$dirty);
}
$scope.AddRole = function(){
console.log("Role name dirty check: "+ $scope.profileform.Name.$dirty);
console.log("Description dirty check: "+ $scope.profileform.Description.$dirty);
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="exApp" ng-controller="ctrl">
<form name="frm" novalidate ng-submit="sub()">
<input type="text" name="yourname" ng-model="name">
<input type="email" name="mail" ng-model="emails" required>
<input type="submit">
</form>
<br><br>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Add role
</button>
<form name="profileform">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" id="myModal">
<!-- Modal content-->
<div class="modal-content" style="margin-top:135px">
<div class="modal-header">
<h4 class="modal-title pull-left"> Add New Role</h4>
<button type="button" class="close pull-right"
data-dismiss="modal" aria-hidden="true">
x
</button>
</div>
<div class="modal-body">
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong>Role<span style="color:green;">*</span></strong></h6>
</div>
<div class="col-xs-9">
<input type="text" name="Name" class="form-control" ng-model="Role.RoleName" />
<!--<span class="error" ng-show="profileform.RoleName.$invalid ">Please enter a Role Name</span>-->
</div>
</div>
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong> Description</strong></h6>
</div>
<div class="col-xs-9">
<textarea name="Description" style="width: 100%; max-height: 200px; max-width: 100%;"
ng-model="Role.Description" maxlength="255"></textarea>
</div>
</div>
<div class="row" style="margin-bottom:1%">
<div class="col-xs-3">
<h6><strong>IsActive?</strong></h6>
</div>
<div class="col-xs-9">
<input type="checkbox" name="IsActive" class="form-control" ng-model="Role.IsActive" style="width:20px;" />
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="AddRole()" ng-disabled="profileform.$invalid" data-dismiss="modal">Save</button>
<button class="btn btn-primary" data-dismiss="modal" ng-click="deselect()">Cancel</button>
</div>
</div>
</div>
</div>
</form>
</body>

Related

Using POST values submitted via different elements under the same page

Title could be misleading. I do have a button with onclick event that calls a function to dump some values into POST variable. It also toggles a modal which have a form that POSTs to current document. A brewer example looks like this:
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$_POST = [];
function send_id(var1, var2) {
$_POST['mainpart'] = var1;
$_POST['subpart'] = var2;
console.log($_POST);
};
function evaluate_form() {
alert($_POST['numsales']);
};
function post_test() {
console.log($_POST);
};
})
</script>
</head>
<body>
<!-- Modal -->
<div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="form1" action="" method="POST">
<div class="form-group">
<label for="address">Address for sale:</label>
<textarea class="form-control" id="address" name="address" rows="6"></textarea>
</div>
<div class="form-group">
<label for="numsales">Number of sales:</label>
<input type="number" value="1" class="form-control only-number" id="numsales" name="numsales">
</div>
<div class="form-group">
<label for="quota">Quota:</label>
<input type="number" value="1" class="form-control only-number" id="quota" name="quota">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" onclick="evaluate_form()">Save</button>
</form>
</div>
</div>
</div>
</div>
<div class="container">
<h2>Performance Calculation Form</h2>
<div class="tables">
<div id="1">
<form>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="col-md-10" colspan="2">1. Sales</th>
<th class="col-md-2"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-8">a) Sales to a </td>
<td class="col-md-2">
<p class="text-center">20</p>
</td>
<!-- <td class="col-md-2"><input name = "SCI_adedi" type="number" class="form-control" id="usr"></td> -->
<td style="text-align: center">
<div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal1" style="position: center;" onclick="send_id('1','a')">Add</button>
</div>
</td>
</tr>
<tr>
<td class="col-md-10" colspan="2">Subtotal </td>
<td class="col-md-2"><input type="text" class="form-control" id="usr"></td>
</tr>
</tbody>
</table>
<button type="submit" id="submit" name="submit" value="KAYDET"> Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
modal part:
<!-- Modal -->
<div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="form1" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>" method="POST">
<div class="form-group">
<label for="address">Address for sale:</label>
<textarea class="form-control" id="address" name="address" rows="6"></textarea>
</div>
<div class="form-group">
<label for="numsales">Number of sales:</label>
<input type="number" value="1" class="form-control only-number" id="numsales" name="numsales">
</div>
<div class="form-group">
<label for="quota">Quota:</label>
<input type="number" value="1" class="form-control only-number" id="quota" name="quota">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" onclick="evaluate_form()">Save</button>
</form>
</div>
</div>
</div>
</div>
There is an 'Add' button that onclick event fires up send_id(val1,val2) function. I'm trying to dump table name and row with this send_id() function via POST method onto same page (Don't know if I implemented it correctly). This Add button also pops-up the modal. Modal have a form in it and its save button submits form with post into same page (Again not sure if I did it right way). And now I need to take all these variables and manipulate them to execute a SQL query. So I also defined an onclick event to modal save button to read all variables inside POST and write down a query via evaluate_form. Too see if I can access POST array I've used alert($_POST); but it bring nothing.
This may be a bad implementation but what I want to accomplish is;
Save info about which add buttton is clicked. A variable is needed so I can use it when executing SQL statements(Writing into correct table).
A model should pop after clicking add button.
Modal should include a form and it should be submitted via POST into same page.
After modal submission, all of these variables (variable of save info and modal form variables) could be acceessed via a function so SQL statements could be arranged.
Thanks.

Implement copy button functionality

I have this Bootstrap code which I would like to use to generate address and implement copy button functionality:
<div class="modal fade" id="bitcoinModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="container">
<div class="offset-top-20 text-md-left">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Copy address</h3>
</div>
<div class="section-60 offset-top-35">
<div class="offset-top-20 text-md-center">
<form class="rd-mailform form-inline-custom text-left" data-form-output="form-output-global" data-form-type="subscribe" method="post" action="http://.........">
<div class="form-group form-group-outside">
<div class="input-group">
<label class="form-label form-label-outside text-dark" for="forms-subscribe-email">Bitcoin Address</label>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required"/>
</div>
<div class="input-group-btn">
<button class="btn btn-width-165 btn-primary" type="submit">Copy</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
How I can copy the content from the input item and close the form when the value is copied into the clipboard?
You can use something like https://clipboardjs.com
p.s. To modify the state your site, you can use the success callback to modify the state of your site.
<script src="https://cdn.jsdelivr.net/npm/clipboard#2/dist/clipboard.min.js"></script>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required" />
<input type="button" value="copy" id="copy-button" data-clipboard-text="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy" />
<script>
var clipboard = new ClipboardJS('#copy-button');
var button = document.querySelector('#copy-button')
clipboard.on('success', function(e) {
button.value = "copied"
});
</script>
I showed you how to change the button text. Closing the modal should be similar.. Without knowing any details.. you probably have to remove or add a class to the modal to make it disappear.
For the delay you can use setTimeout() https://www.w3schools.com/jsref/met_win_settimeout.asp

How to create and show a modal with a dynamic form content?

Sorry, I'm not familiar with JQuery, but I will go all out to understand. As the codes below, I'm going to create and show a bootstrap modal with a dynamic input value "id" specifing which item to delete.
<div class="modal fade" id="myDeleteModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Input the password to delete</h2>
</div>
<div class="modal-body">
<form id="modal-form" method="post" action="password_wrong.html" enctype="multipart/form-data" >
<div class="form-group">
<input type="password" class="form-control" id="res-passwd" name="password" required="required" placeholder="Password:"/>
<input type="hidden" name="delete_id" value="{dynamic}"/>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-default" form="modal-form" >Delete</button>
</div>
</div>
The only dynamic place is
<input type="hidden" name="delete_id" value="{dynamic}"/>
How could I do this?
select the input using jQuery and set the value
$("input[name=delete_id]").val(deleteVal);
$('.openmodal').click(function () {
var deleteVal = $(this).attr('data-val');
console.log(deleteVal)
$('#myDeleteModal').modal({
show: true
});
$("input[name=delete_id]").val(deleteVal);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Delete 1
Delete 2
<div class="modal fade" id="myDeleteModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Input the password to delete</h2>
</div>
<div class="modal-body">
<form id="modal-form" method="post" action="password_wrong.html" enctype="multipart/form-data" >
<div class="form-group">
<input type="password" class="form-control" id="res-passwd" name="password" required="required" placeholder="Password:"/>
<input type="hidden" name="delete_id" value="{dynamic}"/>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-default" form="modal-form" >Delete</button>
</div>
</div>
</div>
or use bootstrap show.bs.modal event
$('#myDeleteModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var deleteVal = button.attr('data-val');
console.log(deleteVal)
$(this).find('input[name=delete_id]').val(deleteVal)
})
$('.openmodal').click(function () {
$('#myDeleteModal').modal('show',$(this));
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Delete 1
Delete 2
<div class="modal fade" id="myDeleteModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Input the password to delete</h2>
</div>
<div class="modal-body">
<form id="modal-form" method="post" action="password_wrong.html" enctype="multipart/form-data" >
<div class="form-group">
<input type="password" class="form-control" id="res-passwd" name="password" required="required" placeholder="Password:"/>
<input type="hidden" name="delete_id" value="{dynamic}"/>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-default" form="modal-form" >Delete</button>
</div>
</div>
</div>

Form structure issue in html

Hi everyone just want you guys to check out and see what is wrong with my form tag. What I'm trying to do here is to submit values from 2 main divs, I'm using bootstrap.
This one doesn't work:
<form class="" action="" id="pos_data" method="post">
<div class="input-group barcode">
<input type="text" class="form-control" id="txtSearch" name="txtSearch" placeholder="barcode" autofocus>
<span class='input-group-addon' style='cursor: pointer;' data-toggle='modal' data-target='#myModal'><i class='fa fa-pencil'></i> Edit</span>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">The Value that would be Inputed will be added to the current value.</h4>
</div>
<div class="modal-body">
<input type="text" id="addtoQuantity" class="form-control" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>
</form>
But when I do this
<form class="" action="" id="pos_data" method="post">
<div class="input-group barcode">
<input type="text" class="form-control" id="txtSearch" name="txtSearch" placeholder="barcode" autofocus>
<span class='input-group-addon' style='cursor: pointer;' data-toggle='modal' data-target='#myModal'><i class='fa fa-pencil'></i> Edit</span>
</div>
</form>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">The Value that would be Inputed will be added to the current value.</h4>
</div>
<div class="modal-body">
<input type="text" id="addtoQuantity" class="form-control" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>
EDITED
I'm using ENTER button as submit. so if I click on the edit span the MODAL div would show and I can enter a value on the textbox of that div. after clicking the add button I put on what I want to search on the txtSearch of the input-group then press enter. The second form structure works but I want the value from the text box in the MODAL div to be submitted to. Does this make sense?
Check this .. i think this will help you ..
$(document).on('click','#add-qty-btn',function(){
var qty = $('#addtoQuantity').val();
var txtsearch = $('#txtSearch').val();
alert('TextSearch is: ' + txtsearch + ' - Quantity: ' + qty);
});
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<br /><br />
<form class="" action="" id="pos_data" method="post">
<div class="input-group barcode">
<input type="text" class="form-control" id="txtSearch" name="txtSearch" placeholder="barcode" autofocus>
<span class='input-group-addon' style='cursor: pointer;' data-toggle='modal' data-target='#myModal'><i class='fa fa-pencil'></i> Edit</span>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">The Value that would be Inputed will be added to the current value.</h4>
</div>
<div class="modal-body">
<input type="text" name="addtoQuantity" id="addtoQuantity" class="form-control" />
</div>
<div class="modal-footer">
<button id="add-qty-btn" type="button" class="btn btn-default" data-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>
</form>
If you want the text box from modal to be included so put the end that of the form after the end of the modal div
Else if you want to remove it and you don't want to include it value so remove it on submit with this jquery function
$("form").submit(function() {
$(this).children('#your input id').remove();
});
With CSS give display none to the modal
Then with the button edit modal click event set the modal display to block

Create Post and push to firebase with AngularJS not working

I'm new to AngularJS, and I'm trying to figure out how to get a create post form in a modal to work, and push a post to firebase. I'm pulling my hair out.
Originally, "create post" was a link in the navbar that took you to a separate page (addPost.html) with a form on it.
I wanted to move the Create Post form to a bootstrap modal on the same page (welcome.html).
There is an edit form in a modal, and it works fine when editing an already created post. The delete post also works as well.
I can't figure out what I am doing wrong.
This is my welcome.html:
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="icon" href="http://getbootstrap.com/favicon.ico">
<title>AngularJS & Firebase Web App</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="blog.css" rel="stylesheet">
</head>
<body ng-controller="WelcomeCtrl">
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item active" href="#/welcome">Home</a>
<a class="blog-nav-item " href="#/addPost">Add Post</a>
</nav>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>AngularJS & Firebase App</h1>
</div>
<p class="lead">Welcome home <b>{{username}}</b> !!</p>
<!-- BUTTON I CREATED TO TRIGGER THE CREATE POST MODAL -->
<p><button class="btn btn-xs btn-info" data-toggle="modal" data-target="#createModal">CREATE</button></p>
<!-- list of articles -->
<div class="list-group" ng-repeat="article in articles">
<a href="#" onclick="return false;" class="list-group-item active">
<h4 class="list-group-item-heading">{{article.title}}</h4>
<p class="list-group-item-text">{{article.post}}</p>
<span class="pull-right">
<button class="btn btn-xs btn-info" ng-click="editPost(article.$id)" data-target="#editModal">EDIT</button>
<button class="btn btn-xs btn-warning" ng-click="confirmDelete(article.$id)" data-target="#deleteModal" >DELETE</button>
</span>
</a>
</div>
</div>
<!-- footer -->
<footer class="footer">
<div class="container">
<p class="text-muted"></p>
</div>
</footer>
<!-- create modal -->
<div class="modal fade" id="createModal" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="editModalLabel">Create Post</h4>
</div>
<div class="modal-body">
<form ng-submit="AddPost()">
<div class="form-group">
<label for="recipient-name" class="control-label">Title:</label>
<input type="text" ng-model="article.title" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="control-label">Post:</label>
<textarea ng-model="article.post" class="form-control" id="message-text"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input id="singlebutton" ng-disabled="!article.title || !article.post" name="singlebutton" class="btn btn-primary" type="submit" value="Publish" />
</form>
</div>
</div>
</div>
</div>
<!-- edit modal -->
<div class="modal fade" id="editModal" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="editModalLabel">Update Post</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="recipient-name" class="control-label">Title:</label>
<input type="text" ng-model="postToUpdate.title" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="control-label">Post:</label>
<textarea ng-model="postToUpdate.post" class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" ng-click="update()">Publish</button>
</div>
</div>
</div>
</div>
<!-- delete modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="text-align:center;">
<h4 class="modal-title" style="color:red;" id="deleteModalLabel">You are going to Delete this post forever !!</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="deletePost()">Delete</button>
</div>
</div>
</div>
</div>
</body>
</html>
This is my welcome.js:
'use strict';
angular.module('myApp.welcome', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/welcome', {
templateUrl: 'welcome/welcome.html',
controller: 'WelcomeCtrl'
});
}])
.controller('WelcomeCtrl', ['$scope','$firebase','CommonProp', function($scope,$firebase,CommonProp) {
$scope.username = CommonProp.getUser();
var firebaseObj = new Firebase("https://yyear.firebaseio.com/Articles");
var sync = $firebase(firebaseObj);
$scope.articles = sync.$asArray();
$scope.editPost = function(id) {
var firebaseObj = new Firebase("https://yyear.firebaseio.com/Articles/" + id);
var syn = $firebase(firebaseObj);
$scope.postToUpdate = syn.$asObject();
$('#editModal').modal(); // triggers the modal pop up
};
$scope.update = function() {
var fb = new Firebase("https://yyear.firebaseio.com/Articles/" + $scope.postToUpdate.$id);
var article = $firebase(fb);
article.$update({
title: $scope.postToUpdate.title,
post: $scope.postToUpdate.post,
emailId: $scope.postToUpdate.emailId
}).then(function(ref) {
$('#editModal').modal('hide');
}, function(error) {
console.log("Error:", error);
});
};
$scope.confirmDelete = function(id) {
var fb = new Firebase("https://yyear.firebaseio.com/Articles/" + id);
var article = $firebase(fb);
$scope.postToDelete = article.$asObject();
$('#deleteModal').modal();
};
$scope.deletePost = function() {
var fb = new Firebase("https://yyear.firebaseio.com/Articles/" + $scope.postToDelete.$id);
var article = $firebase(fb);
article.$remove().then(function(ref) {
$('#deleteModal').modal('hide');
}, function(error) {
console.log("Error:", error);
});
};
$scope.AddPost = function(){
console.log("This was called.");
var title = $scope.article.title;
var post = $scope.article.post;
var firebaseObj = new Firebase("https://yyear.firebaseio.com/Articles");
var fb = $firebase(firebaseObj);
fb.$push({ title: title,post: post,emailId: CommonProp.getUser() }).then(function(ref) {
console.log(ref);
//$location.path('/welcome');
}).then(function(ref) {
$('#editModal').modal('hide');
}, function(error) {
console.log("Error:", error);
});
};
}]);
The original Create Post form in addPost.html that works looks like this:
<body ng-controller="AddPostCtrl">
<div class="blog-masthead">
<div class="container">
<nav class="blog-nav">
<a class="blog-nav-item " href="#/welcome">Home</a>
<a class="blog-nav-item active" href="#/addPost">Add Post</a>
</nav>
</div>
</div>
<div class="container" >
<form class="form-horizontal" ng-submit="AddPost()">
<fieldset>
<legend>Create Post</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="txtTitle">Title</label>
<div class="col-md-4">
<input id="txtTitle" name="txtTitle" ng-model="article.title" type="text" placeholder="placeholder" class="form-control input-md">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="txtPost">Post</label>
<div class="col-md-4">
<textarea class="form-control" id="txtPost" ng-model="article.post" name="txtPost" ></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<input id="singlebutton" ng-disabled="!article.title || !article.post" name="singlebutton" class="btn btn-primary" type="submit" value="Publish" />
</div>
</div>
</fieldset>
</form>
</div><!-- /.container -->
The addPost.js file looks like this.
'use strict';
angular.module('myApp.addPost', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/addPost', {
templateUrl: 'addPost/addPost.html',
controller: 'AddPostCtrl'
});
}])
.controller('AddPostCtrl', ['$scope','$firebase','$location','CommonProp',function($scope,$firebase,$location,CommonProp) {
$scope.AddPost = function(){
var title = $scope.article.title;
var post = $scope.article.post;
var firebaseObj = new Firebase("https://yyear.firebaseio.com/Articles");
var fb = $firebase(firebaseObj);
fb.$push({ title: title,post: post,emailId: CommonProp.getUser() }).then(function(ref) {
console.log(ref);
$location.path('/welcome');
}, function(error) {
console.log("Error:", error);
});
}
}]);
I assumed that if I copied the the addPost function from addPost.js to the welcome.js file and then duplicated the edit form in welcome.html and modified it so it was similar to the Create Post form in addPost.html it would work.
Obviously it doesn't, but I don't know what I am doing wrong.
The modal pops up but the publish button doesn't do anything
I figured it out. I had my closing form tag in the wrong spot first of all in my create post modal.
The Create Post modal should be like this:
<div class="modal fade" id="createModal" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form ng-submit="AddPost()">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="editModalLabel">Create Post</h4>
</div>
<div class="modal-body">
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="txtTitle">Title</label>
<input id="txtTitle" name="txtTitle" ng-model="article.title" type="text" placeholder="placeholder" class="form-control input-md">
</div>
<!-- Textarea -->
<div class="form-group">
<label class="control-label" for="txtPost">Post</label>
<textarea class="form-control" id="txtPost" ng-model="article.post" name="txtPost" ></textarea>
</div>
<!-- Button -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input id="singlebutton" ng-disabled="!article.title || !article.post" name="singlebutton" class="btn btn-primary" type="submit" value="Publish" />
</div>
</form>
</div>
</div>
</div>
My addPost function in welcome.js should be like this:
$scope.AddPost = function(){
console.log("This was called.");
var title = $scope.article.title;
var post = $scope.article.post;
var firebaseObj = new Firebase("https://yyear.firebaseio.com/Articles");
var fb = $firebase(firebaseObj);
fb.$push({ title: title,post: post,emailId: CommonProp.getUser() }).then(function(ref) {
console.log(ref);
//$location.path('/welcome');
}).then(function(ref) {
$('#createModal').modal('hide');
}, function(error) {
console.log("Error:", error);
});
};