How to fetch output to another page using AngularJS watch - angularjs

I Create two controller one is parent and another one is child i want to create form all code of form in parent html and when i click on submit button output will Show on Child page only output will show on child page.
This is my Controller
var myApp = angular.module("myApp", []);
myApp.controller("nameCtrl", ['$scope', function ($scope) {
$scope.userData = "";
$scope.getData = function (user) {
$scope.userData = angular.copy(user);
};
$scope.$watch('userData', function (newValue) {
$scope.$broadcast('nameChanged', newValue);
console.log(userData);
});
}]);
myApp.controller('ChildController', ['$scope', function ($scope) {
$scope.$on('nameChanged', function (event, value) {
$scope.userData = value;
});
}]);
This is my Parent html
<div ng-controller="nameCtrl">
<div style="border: 1px solid black;">
<form>
First Name:<input type="text" ng-model="user.fname" /><br /><br />
Last Name:<input type="text" ng-model="user.lname" /><br /><br />
Address :<input type="text" ng-model="user.adress" /><br /><br />
<input type="button" value="SUBMIT" ng-click="getData(user)" />
</form>
</div>
<br /><br />
<br /><br />
<div style="border: 1px solid black;">
<div data-ng-controller="ChildController">
<p>First Name: {{userData.fname}}</p>
<p>Last Name: {{userData.lname}}</p>
<p>Address: {{userData.adress}}</p>
</div>
</div>
</div>
I want this ChildController output will shown on another page like child page
<div style="border: 1px solid black;">
<div data-ng-controller="ChildController">
<p>First Name: {{userData.fname}}</p>
<p>Last Name: {{userData.lname}}</p>
<p>Address: {{userData.adress}}</p>
</div>
</div>

Related

How to bind data in table using AngularJs?

I have a Simple form like Below
<form novalidate name="f1" ng-submit="SaveData(User)">
<b>Fname</b><input type="text" ng-model="User.fname" /><br />
<b>Lnamew</b><input type="text" ng-model="User.lname" /><br />
<input type="submit" />
</form>
And my controller is
app.controller('HomeCtrls', function ($scope) {
$scope.msg = "Ok...."
$scope.Getdata = [];
$scope.SaveData = function (data) {
$scope.Getdata.push(data)
}
})
Here i wana to bind Getdata in table Like
<tr ng-repeat="formdata in Getdata">
<td>{{formdata.User.fname}}</td>
<td>{{formdata.User.lname}}</td>
this is your answer with a little changes
var app = angular.module('formApp',[]);
app.controller('formController',function($scope){
$scope.Getdata = [];
$scope.SaveData = function (data) {
$scope.Getdata.push({
fname:$scope.fname,
lname:$scope.lname
})
console.log($scope.Getdata)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="formApp" ng-controller="formController">
<form novalidate name="f1" ">
<b>Fname</b><input type="text" ng-model="fname" /><br />
<b>Lnamew</b><input type="text" ng-model="lname" /><br />
<button type="button" ng-click="SaveData()">Save</button>
</form>
</div>

redirecting to another page using angularjs on button click

I have this .cshtml page to login
<main id="login" ng-controller="loginCtrl">
<div id="page" class="full-screen min-height">
<div id="page-inner">
<div id="login-page-content" class="min-height-inner container-padding">
<div class="container center text-center">
<h1>
<label>Login</label>
</h1>
<div id="login-form">
<div>
<input type="email" id="Username" placeholder="UserName/Email" class="login-input" required="required" />
</div>
<div>
<input type="password" id="Username" placeholder="Password" class="login-input" required="required" />
</div>
<div>
</div>
<div class="cf">
<input type="submit" value="Login" class="login-submit" ng-click="clicked()"/>
</div>
</div>
Forgot Details ?
</div>
</div>
</div>
</div>
and this is my controller.js
angular.module('userManagement')
.controller('loginCtrl', ['$scope', '$http','$window', function ($scope, $http,$window) {
$scope.clicked = function () {
window.location = '#/join';
}
}
]);
and also my app.js
$routeProvider.caseInsensitiveMatch = true;
$routeProvider.when('/login', {
templateUrl: 'Account/login',
controller: 'loginCtrl'
});
I want to be able to go to another page when i click the button, vallidations are not important for now I just want the buttons to go to the page when i click a button, with the above code it takes me to
http://localhost:52653/Account#!/login#%2Fjoin
You should use the angular route system or ui.router :
ngRoute and location
$location.path( "#/join" );
ui.router
$state.go(path_to_join_state);

angularjs form inside bootstrap modal popup

I have a form inside a bootstrap modal.But I can't able to get the values of form on controller side with scope variable.
https://plnkr.co/edit/FjKXUpoBDdvQqomI97ml?p=preview
My original code has another problem also. when I click on submit button it will refresh the whole page and submit function is not executing.
My form:
<div class="modal-body">
<form class="form-horizontal" name="contact" ng-submit="contactForm()">
<div class="form-group">
<label class="col-lg-3 control-label">Name* :</label>
<div class="col-lg-8">
<input class="form-control" type="text" id="name" name="_name" ng-model="_name" > </div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email* :</label>
<div class="col-lg-8">
<input class="form-control" type="email" id="email" name="_email" ng-model="_email" required> </div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Mobile* :</label>
<div class="col-lg-8 row">
<div class="col-lg-4">
<input type="text" class="form-control" ng-model="_cc" placeholder="+91" name="_cc"> </div>
<div class="col-lg-8">
<input class="form-control" type="text" ng-model="_mobile" maxlength="10" ng-pattern="/^[0-9]{5,10}$/" id="mobile" name="_mobile"></div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Message :</label>
<div class="col-lg-8">
<textarea class="form-control" rows="2" name="_condition" ng-model="_condition"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-7 col-lg-5">
<button type="submit" class="btn btn-primary" id="contactSubmit" >Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div></div>
</form>
</div>
Controller:
$scope.contactForm = function(){
console.log($scope._condition,$scope._name,$scope._email,$scope._cc,$scope._mobile);
};
You are opening the modal using plain jQuery approach which is not going to work in Angular, because opened modal is not connected to Angular application, so it doesn't know that modal has to be handled, HTML parsed, etc.
Instead you should use directives properly, or in case of modal dialog you can simply use existent ones, like Angular UI project, which brings ready Bootstrap directives for Angular. In your case you need $modal service and inject the $http service to have your data posted.
Here is the working plunker using angular-ui bootstrap.
PLUNKER:https://plnkr.co/edit/rjtHJl0udyE0PTMQJn6p?p=preview
<html ng-app="plunker">
<head>
<script src="https://code.angularjs.org/1.2.18/angular.js"></script>
<script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3>I'm a modal!</h3>
</div>
<form ng-submit="submit()">
<div class="modal-body">
<label>Email address:</label>
<input type="email" ng-model="user.email" />
<label>Password</label>
<input type="password" ng-model="user.password" />
</div>
<div class="modal-footer">
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
<input type="submit" class="btn primary-btn" value="Submit" />
</div>
</form>
</script>
<button class="btn" ng-click="open()">Open Modal</button>
</div>
</body>
</html>
JS:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log,$http) {
$scope.user = {
email: '',
password: null,
};
$scope.open = function () {
$modal.open({
templateUrl: 'myModalContent.html', // loads the template
backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window
windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template
controller: function ($scope, $modalInstance, $log, user) {
$scope.user = user;
$scope.submit = function () {
$log.log('Submiting user info.'); // kinda console logs this statement
$log.log(user);
$http({
method: 'POST',
url: 'https://mytesturl.com/apihit',
headers: {
"Content-type": undefined
}
, data: user
}).then(function (response) {
console.log(response);
$modalInstance.dismiss('cancel');
}, function (response) {
console.log('i am in error');
$modalInstance.dismiss('cancel');
});
//$modalInstance.dismiss('cancel'); // dismiss(reason) - a method that can be used to dismiss a modal, passing a reason
}
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
resolve: {
user: function () {
return $scope.user;
}
}
});//end of modal.open
}; // end of scope.open function
};

angularjs: Form entry problems

I have this dynamic form which entries I would like to save. The problem is that the saved entry is like this [{"name":"Noora","":{"Gender":"Woman","Address":"filler address"}}] I'm curios why the app saves the info after name like a nameless list. Name is hardcoded input and the other two (Gender and Address) can be dynamically added when using the program.
Here is the form entry html part:
<form>
<h2>Sign in:</h2>
<div class="form-group">
<label for="eventInput">Name</label>
<input style="width: 200px;" type="text" class="form-control" id="eventInput" data-ng-model="newEntry.name">
</div>
<div data-ng-repeat="field in event.fields">
<div class="form-group">
<label for="{{$index + 1}}">{{field.name}}</label>
<input style="width: 200px;" type="text" class="form-control" id="{{$index + 1}}" data-ng-model="newEntry.[field.name]">
</div>
</div>
<span>{{entries}}</span>
<div class='wrapper text-center'>
<div class="form-group">
<div class="col-lg-4 col-lg-offset-4">
<button style="width: 100px;" data-ng-click="addEntry()" class="btn btn-primary">Enroll</button>
<p></p>
<button style="width: 100px;" data-ng-click="back()" class="btn btn-primary">Back</button>
</div>
</div>
</div>
</form>
and here is the controller:
App.controller('aboutController', function($scope, $location, $routeParams, eventService) {
$scope.event = eventService.getCustomers()[$routeParams.id];
$scope.back = function() {
$location.path('/');
};
$scope.addEntry = function() {
$location.path('/');
$scope.event.entries.push($scope.newEntry);
};
});
I would like to either be able to name the child list or just record the entries into a continuous list. Any idea how would it be possible?
Br,
Norri

view not updating in AngularJS after $http.get

After getting values from REST-API $scope is not updating view
here is my HTML
<div ng-repeat="s in services1">
<label class="checkbox-inline">
<input type="checkbox" id="{{s.Id}}" name="{{s.Id}}" ng-model="s.IsChecked">
{{s.Name}}</label>
<br/>
</div>
AngularJS Controller
var addMember = [
'$scope', '$http', function ($scope, $http) {
$scope.member = [];
$scope.services1 = [];
var bid = "";
//onclick function
$scope.setId = function(id) {
$("#processing-modal").modal('show');
$http.get('/api/Servicesapi/ServicesByBusiness/' + id)
.then(function (response) {
$scope.services1 = response.data;
$("#processing-modal").modal('hide');
$("#anm").modal('show');
console.log($scope.services1); //this is printing values
},
function() {
alert("Error in getting services of this Employee");
});
console.log($scope.services1); //this prints empty array
};
}
];
first console.log() prints array with values but the other one outside $http.get function prints empty braces and view is not updating
UPDATE
FULL HTML VIEW
<div ng-controller="addMember">
<div class="savebar"><button type="submit" class="btn btn-warning" value="Save"
id="anmbutton" ng-click="setId('#ViewBag.bid');">Add New Members</button>
</div>
</div>
<!--Add new Members Modal -->
<div ng-controller="addMember">
<div class="modal fade" id="anm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="width: 790px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Team Member</h4>
</div>
<div class="modal-body">
<div style="float: left">
<div style="float: left">
<div class="container" style="border-right: 1px solid gray; margin-left: -20px; margin-top: -12px; width: 440px;">
<!-- Profile Picture -->#*<br/>*#
<div style="border: 1px solid; float: left; height: 100px; width: 100px;">
<img src="" />
<a class="badge" href="#">Add Picture</a>
</div>
<div style="float: left; height: 100px; margin-left: 20px; width: 200px;">
<br/><br/><br/><br/><br/>
<input class = "form-control" style = "margin-top: -100px; width: 250px" type="text" value="" id="membername" ng-model="member.Name"/>
<input type="checkbox" class="checkbox" id="provideservice" name="provideservice" value="true"/> Provide Services<br/>
<input type="checkbox" class="checkbox" id="SearchedByName" name="SearchedByName" value="true" ng-model="member.SearchedByName"/> Allow Selected by Name
<hr/>
</div>
</div>
<div style="border-right: 1px solid grey; margin-top: -11px; width: 420px;">
<div style="margin-left: 112px; margin-top: 10px; width: 200px;">
<label>Pricing Level</label>
<input class="form-control" type="text" id="pricinglevel" name="pricinglevel"/>
<label>Job Title</label>
<input class="form-control" type="text" id="JobTitle" name="JobTitle" value="" ng-model="member.JobTitle"/>
<label>Phone</label>
<input class="form-control" type="text" id="Phone" name="Phone" value="" ng-model="member.Phone"/>
<label>Gender</label>
<br/>
<label class="checkbox-inline">
<input type="radio" id="Gender" name="Gender" value="Male" ng-model="member.Gender"> Male
</label>
<label class="checkbox-inline">
<input type="radio" id="Gender" name="Gender" value="Female" ng-model="member.Gender"> Female
</label>
<label>Email</label>
<input class="form-control" type="text" id="Email" name="Email" value="" ng-model="member.Email"/>
<label class="checkbox-inline">
<input type="checkbox" id="CanLogin" name="CanLogin" ng-model="member.CanLogin"> Login to Dashboard
</label>
<br/>
<label>about</label>
<textarea class="form-control" name="About" ng-model="member.About" ></textarea>
</div>
</div>
</div>
<div style="float: right; /*padding-right: 20px;*/margin-right: -345px; margin-top: -16px; width: 340px;">
<label>What services can be booked for this employee online?</label>
<br/>
<div ng-repeat="s in services1">
<label class="checkbox-inline">
<input type="checkbox" id="{{s.Id}}" name="{{s.Id}}" ng-model="s.IsChecked"> {{s.Name}}
</label>
<br/>
</div>
<pre>$scope.services1 {{services1 | json}}</pre>
</div>
</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<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="addNew()" data-dismiss="modal">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
UPDATE:
I reproduced two controllers declarations with same name.
<div ng-controller="addMember">
<button ng-click="setId()">SDFS</button>
</div>
<div ng-controller="addMember">
<p ng-repeat="s in services">{{s}}</p>
</div>
ng-repeat doesn't work in second controller declaration. If I move <p ng-repeat="s in services">{{s}}</p> to top controller, it works. Do not declare two controllers with one name :)
END
console.log($scope.services1); //this prints empty array
This is because $http.get() promise is not resolved yet.
Try this code
var addMember = [
'$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
$scope.member = [];
$scope.services1 = [];
var bid = "";
//onclick function
$scope.setId = function(id) {
$("#processing-modal").modal('show');
$http.get('/api/Servicesapi/ServicesByBusiness/' + id)
.then(function (response) {
$timeout(function(){
$scope.services1 = response.data;
});
$("#processing-modal").modal('hide');
$("#anm").modal('show');
console.log($scope.services1); //this is printing values
},
function() {
alert("Error in getting services of this Employee");
});
console.log($scope.services1); //this prints empty array
};
}
];
Or call $scope.$apply() after $scope.services1 = response.data;
$scope.services1 = response.data;
$scope.$apply()

Resources