$scope.watch doesn't work in ionic framework - angularjs

$scope.watch doesn't work in ionic framework
I was designing some fliter in ionic..It won't work ..the same code is working properly on blank test app as well as angularjs...
My html
<div class="bar bar-header bar-royal">
<div class="h1 title">Hotel search</div>
</div>
<ion-view>
<ion-content>
<h4 style="margin-top:100px;color:#116262;margin-left:10px;font-weight:bold"><i class="icon ion-edit"></i> Request Form</h4>
<div class="list" style="margin-top:15px;">
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
City
</div>
<select ng-model="formData.city">
<option value="Chennai">Chennai</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Goa">Goa</option>
</select>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">CheckIn</span>
<input type="date" ng-model="formData.checkIn" value="{{formData.checkIn | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">checkOut</span>
<input type="date" ng-model="formData.checkOut" value="{{formData.checkOut | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Rooms
</div>
<select ng-model="formData.room">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Adults
</div>
<select ng-model="formData.adult">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<div class="row center" style="margin-top:20px;">
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
</div>
{{formData}}
<input type="text" ng-model="useStars"/>
{{fooChanges}}
</div>
</ion-content>
</ion-view>
My controller is
.controller('PlaylistsCtrl', function($scope,$state) {
$scope.formData ={};
$scope.formData.checkIn = new Date();
$scope.formData.checkOut= new Date();
$scope.formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
$scope.formData.room = 1;
$scope.formData.city = 'Chennai';
$scope.formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
})
what's problem on above code ?? ..I tried lot of times..it won't wrk..if have any alternative idea's??

As said in comment the problem was resolved doing the following :
<input type="text" ng-model="$parent.useStars"/>
However the best way to never have this problem : always use an intermdediary object like $scope.data.userStarts. So you won't ever need the $parent trick, this is called dot notation in Angular and is due to limit of javascript inheritance.

In your button you are passing the formData as parameter
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
in your controller there is not formData parameter is refereed except in your search button in the same way you should pass the useStars .
.controller('PlaylistsCtrl', function($scope,$state) {
formData ={};
formData.checkIn = new Date();
formData.checkOut= new Date();
formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
formData.room = 1;
formData.city = 'Chennai';
formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
$state.go('app.search',{formData:formData});
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
});
Here you don't need $scope before form angularjs itself create a scope variable if some event occurs.
but there is no event is occurring so there is no way for PlaylistCtrl can understand $scope.formData so try to make an event to be appeare or try to understand the $scope.
so that will help for two way databinding

Related

Ionic1 Edit Record & Populate Fields

I have an Ionic1 application which has a function which allows the user to edit a record. For example, change a contact from prospect to customer.
When I click edit on the prospect, this loads a view for editing the prospect.
My application then shows a form and populates the fields with the prospect data after a $http.get request allowing the user to amend / update the prospect as required.
My problem, when clicking edit only some of the dropdown lists are populating.
My controller for editing a prospect
.controller('prospectCtrlEdit', function($scope, $http, $state, $stateParams, $ionicLoading, prospects, customers) {
$ionicLoading.show();
$http.get('URL_API_END_POINT/' + $stateParams.customer + '/edit?token=' + localStorage.getItem("token")).then(function(successResponse){
$scope.return = successResponse;
$scope.prospect = $scope.return.data.data.prospect;
$ionicLoading.hide();
}, function(errorRepsonse){
$scope.error = errorRepsonse;
$scope.$broadcast('scroll.refreshComplete');
$ionicLoading.hide();
});
});
My form
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Type
</div>
<select ng-model="prospect.client_type">
<option value="2">Prospect</option>
<option value="1">Customer</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">
Title
</div>
<select ng-model="prospect.title">
<option value="Mr and Mrs">Mr & Mrs</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
</select>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" placeholder="First name" ng-model="prospect.first_name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Last Name</span>
<input type="text" placeholder="Last name" ng-model="prospect.last_name">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>
<input type="email" placeholder="Email address" ng-model="prospect.email">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Mobile</span>
<input type="tel" placeholder="Mobile" ng-model="prospect.mobile">
</label>
<div class="item">
<button class="button button-block" ng-click="updateProspect()">Update Profile</button>
</div>
</div>
For reference, I've included my response from the API which is what is being used to populate the form.
{
"data":{
"status":"success",
"prospect":{
"id":5887,
"user_id":9,
"title":"Mr and Mrs",
"company":null,
"first_name":"Peter",
"last_name":"Prospect",
"email":"",
"mobile":"",
"telephone":null,
"addressline1":null,
"addressline2":null,
"addressline3":null,
"addressline4":null,
"lat":null,
"long":null,
"town":null,
"postcode":null,
"county":null,
"country":null,
"comments":null,
"status":1,
"created_at":"2017-07-12 20:52:48",
"updated_at":"2017-07-12 20:52:48",
"client_type":2,
"prospect_converted":null
}
}
}
To recap. When the edit prospect page loads the form is populated with
all the current data from the above $http.get() request. The user
updates the information as desired - the problem is dropdowns like
"Type" for determining the type either Customer or Prospect isn't auto
populating however the title is.

Submitting multiple check-box selection in ASP.NET MVC form with Angular JS

I have a form with textboxes, dropdownlists and checkboxes. I have the challenge of submitting several checkbox items into the database. The data did submit but it only submits the last checked item on the form.
Here's is my UI
<div class="block type-3">
<div class="container">
<div class="row post animated fadeInUp">
<div class="col-xs-12 form-block">
<div ng-controller="tutorequestController">
<form name="tutrequestform" novalidate>
<div class="form-text">Required fields are <span class="text-blue">*</span>. Fill out the form and we'll contact you soon</div>
<div class="row">
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.firstname" name="firstname" type="text" class="form-input" placeholder="Firstname *" required />
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.lastname" name="lastname" type="text" class="form-input" placeholder="Lastname *" required />
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.phonenumber" name="phonenumber" type="number" class="form-input" placeholder="Phone Number *" required />
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.phonenumber2" name="phonenumber2" type="number" class="form-input" placeholder="Re-type Phone Number *" required />
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.emailaddress" name="emailaddress" type="email" class="form-input" placeholder="Email Address *" required />
</div>
<div>
<div ng-controller="stateLGAController">
<div class="col-xs-12 col-sm-6">
<!--<select ng-change="GetStateLgas()" ng-options="moreState as moreState.state1 for moreState in moreStates track by moreState.state_id" ng-model="select" class="form-input"><option>Select Your State</option></select>-->
<select ng-model="tutorRequest.state_id" ng-change="GetStateLgasByid(tutorRequest.state_id)"
ng-options="moreState.state_id as moreState.state1 for moreState in moreStates" class="form-input">
<option>Select Your State</option>
</select><!---->
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.lga_id"
ng-options="lga.lga_id as lga.local_govt for lga in stateLGA" class="form-input">
<option>Select Your L.G.A</option>
</select>
</div>
</div>
</div>
<div class="col-xs-12">
<textarea ng-model="tutorRequest.address" class="form-input" name="address" placeholder="House Address *" required></textarea>
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.numofchild" name="numofchild" class="form-input">
<option value='Select-Number-of-Child-for-Tutor'>Select number of Child/ren for Tutor</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.classrangeandage" class="form-input" name="classrangeandage" type="text" required placeholder="Class range of each child and age respectively *" />
</div>
<div class="col-xs-12 form-text">
<span class="text-blue">What subject would the tutor teach? Tick appropriate subject below.</span>
</div>
<div ng-app="app" ng-controller="subjectController">
<div ng-repeat="sub in tutorRequest.tutorsubject" class="col-xs-12 col-sm-4">
<div class="chBoxPad">
<input ng-model="tutorRequest.tutorsubject[$index].checked" type="checkbox" id="{{sub.sub_id}}" name="{{sub.subject1+'_'+$index}}" ng-change="updateChecked()" />
<label for="{{sub.sub_id}}"><span></span>{{sub.subject1}}</label>
</div>
</div>
</div>
<div class="col-xs-12">
<select ng-model="tutorRequest.preferredsexoftutor" name="sex" id="sex" class="form-input">
<option value="preferredsexoftutor" selected="selected">Preferred Sex of Tutor</option>
<option value="any">Any</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.childcurrentschool" name="childcurrentschool" type="text" class="form-input" placeholder="Current School of Child *" required />
</div>
<div class="col-xs-12">
<select ng-model="tutorRequest.schoolcurriculum" name="schoolcurriculum" id="curi" class="form-input">
<option value="">What curriculum does the school use</option>
<option value="Not Sure">Not Sure</option>
<option value="British Curriculum">British Curriculum</option>
<option value="American Curriculum">American Curriculm</option>
<option value="Nigerian Curriculum">Nigerian Curriculum</option>
<option value="Combination of British and Nigerian">Combination of British and Nigerian</option>
<option value="Question doesnt apply to me">Question doesn't apply to me</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.urgencyoftutor" name="urgencyoftutor" id="urgencyoftutor" class="form-input">
<option value="">Urgency of tutor</option>
<option value="Urgently" selected="selected">Urgently</option>
<option value="In 1 Week">In a Week</option>
<option value="In 2 Weeks">In 2 Weeks</option>
<option value="In a Month">In a Month</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.calltime" name="calltime" id="calltime" class="form-input">
<option value="" selected="selected">What time of the day would you like to be called</option>
<option value="Anytime">Anytime</option>
<option value="8 to 12">Morning - between 8am and 12noon</option>
<option value="12 to 3">Afternoon - between 12noon to 3pm</option>
<option value=" 3 to 6">Evening - between 3pm to 6pm</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.frequencyoftutor" name="frequencyoftutor" id="frequencyoftutor" class="form-input">
<option value="">How many times a week would you like the tutor to come</option>
<option value="1">Once a week</option>
<option value="2">2 times a week</option>
<option value="3">3 times a week</option>
<option value="4">4 times a week</option>
<option value="5">5 times a week</option>
<option value="6">6 times a week</option>
<option value="7">7 times a week</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.tutorhrs" name="tutorhrs" id="tutorhrs" class="form-input">
<option value="">How many hours per day should tutoring hold</option>
<option value="1" selected="selected">1 hr</option>
<option value="2">2 hrs</option>
<option value="3">3 hrs</option>
<option value="4">4 hrs</option>
<option value="5">5 hrs</option>
<option value="6">6 hrs</option>
<option value="7">7 hrs</option>
</select>
</div>
<div class="col-xs-12 col-sm-6">
<input ng-model="tutorRequest.goal" name="goal" type="text" class="form-input" placeholder="Specific goal for tutoring session" required />
</div>
<div class="col-xs-12 col-sm-6">
<select ng-model="tutorRequest.howdidyouhearaboutus" name="howdidyouhearaboutus" class="form-input">
<option value="">How did you hear about us</option>
<option value="Google" selected="selected">Google</option>
<option value="Facebook">Facebook</option>
<option value="I got an SMS from Prepschool">I got an SMS from Prepschool</option>
<option value="Twitter">Twitter</option>
<option value="Nairaland">Nairaland</option>
<option value="I saw a flyer">I saw a flyer</option>
<option value="Prepschool Brochure">Prepschool Brochure</option>
<option value="A friend / member of household">A friend / member of household</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="col-xs-12">
<span class="button">
<button type="submit" class="submit" ng-click="CreateTutRequest(tutorRequest, tutrequestform.$valid)">Submit Request</button>
</span>
<!--<td><input type="submit" ng-click="CreateEmployee(Emp, myForm.$valid)" value="Create" /></td>
<td><input type="submit" ng-click="UpdateEmployee(Emp)" value="Update" /></td>-->
<span class="success"></span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
And here's the a partial graphical representation of the UI in image format.
And here's my angularJs controller for the page.
myApp.factory('crudServiceTutorrequest', function ($http) {
// Create an object and start adding methods to the object.
crudTutRObj = {};
// Add create method to the crudTutRObj
crudTutRObj.getAll = function () {
var tutorRequests;
tutorRequests = $http({ method: 'GET', url: '/Tutorrequest/Index' }).
then(function (response) {
return response.data;
});
return tutorRequests;
}
crudTutRObj.getStates = function () {
var ddlStates;
ddlStates = $http({ method: 'GET', url: '/States/Index' }).
then(function (response) {
return response.data;
});
return ddlStates;
}
crudTutRObj.getSubject = function () {
var Subjects;
Subjects = $http({ method: 'GET', url: '/Subject/Index' }).
then(function (response) {
return response.data;
});
return Subjects;
}
crudTutRObj.createTutRequest = function (tutRequest) {
var tutorRequest;
tutorRequest = $http({ method: 'POST', url: '/Tutorrequest/Create', data: tutRequest }).
then(function (response) {
return response.data;
});
return tutorRequest;
}
crudTutRObj.getById = function (id) { }
crudTutRObj.update = function (fms) { }
crudTutRObj.deleteById = function (id) { }
return crudTutRObj;
});
myApp.controller('tutorequestController', function ($scope, crudServiceTutorrequest) {
// Get all tutorRequests
//crudServiceTutorrequest.getAll().then(function (result) {
// $scope.tutorRequests = result;
//})
// Get data for states to populate the states dropdownlist
crudServiceTutorrequest.getStates().then(function (result) {
$scope.moreStates = result;
})
// Submit the form with the create function
$scope.CreateTutRequest = function (tutorRequest) {
crudServiceTutorrequest.createTutRequest(tutorRequest).then(function (result) {
$scope.Msg = "Tutor Request has been submitted successfully!";
});
}
});
myApp.controller('stateLGAController', function ($scope, $http) {
$scope.GetStateLgas = function () {
$http({ method: 'Get', url: '/StateLGA/Index' })
.then(function (response) {
$scope.stateLGA = response.data;
});
};
$scope.GetStateLgasByid = function (state_id) {
$http({ method: 'Get', url: '/StateLGA/GetlgaByStateid/' + state_id })
.then(function (response) {
$scope.stateLGA = response.data;
});
};
});
angular.module("app", []).controller('subjectController', ['$scope', function ($scope, crudServiceTutorrequest) {
var subjects;
subjects = crudServiceTutorrequest.getSubject().then(function (result) {
$scope.subjects = result;
})
$scope.tutorRequest = {
tutorsubject: subjects
};
angular.forEach($scope.subjects, function (subject) {
var sub = angular.merge({ checked: false }, subject);
$scope.tutorRequest.tutorsubject.push(sub);
});
$scope.allSubjectChecked = [];
$scope.updateChecked = function () {
console.log($scope.tutorRequest.tutorsubject);
$scope.allSubjectChecked = [];
angular.forEach($scope.tutorRequest.tutorsubject, function (sub) {
if (sub.checked) {
$scope.allSubjectChecked.push(sub);
}
});
}
}]);
And finally, the js file is routes all the pages to the ng-view
var myApp = angular.module("myApp", ['ngRoute']);
myApp.config(function ($routeProvider) {
$routeProvider.when('/Department', {
templateUrl: 'Templates/Admin/Department/Department.html',
controller: 'departmentController'
});
$routeProvider.when('/Employee', {
templateUrl: 'Templates/Admin/Employee/Employee.html',
controller: 'employeeController'
});
$routeProvider.when('/Aboutus', {
templateUrl: 'Templates/User/Aboutus/Aboutus.html',
controller: ''
});
$routeProvider.when('/Contactus', {
templateUrl: 'Templates/User/Contactus/Contactus.html',
controller: ''
});
$routeProvider.when('/Tutorregistration', {
templateUrl: 'Templates/User/Tutorregistration/Tutorregistration.html',
controller: ''
});
$routeProvider.when('/Tutorrequest', {
templateUrl: 'Templates/User/Tutorrequest/Tutorrequest.html',
controller: 'tutorequestController'
});
$routeProvider.otherwise({
redirectTo: '/Home',
templateUrl:'Templates/User/Home/Home.html'
});
});
Would appreciate your help with this.
The problem might be that MVC doesn't bind a checkbox value if it's false. Try this instead:
<div ng-controller="subjectController">
<div ng-repeat="sub in subjects" class="col-xs-12 col-sm-4">
<div class="chBoxPad">
<input ng-model="tutorRequest.tutorsubject" type="checkbox" id="{{sub.sub_id}}" name="{{sub.subject1}}" ng-true-value="{{sub.sub_id}}" />
<input type="hidden" value="false" name="{{sub.subject1}}" />
<label for="{{sub.sub_id}}"><span></span>{{sub.subject1}}</label>
</div>
</div>
</div>
If the checkbox is false, the hidden field will be submitted. Instead, when it's true, two fields will be submitted at the same time (false and true) and MVC will get that as a "true" value.
I know it sounds odd, but that it how MVC works. You can see that yourself if you use #Html.CheckBoxFor(). You'll have the same output.
The problems lie in the fact that you put the same ng-model for every subject. Angular don't build an array for a list of checkboxes, you have to bind every checkbox to a separate model.
The best way of doing that is to initialize you tutorsubject with all the values possible and a field 'checked' set to false like this :
angular.module("app", []).controller("ctrl", ['$scope', function($scope){
$scope.tutorRequest = {tutorsubject : [{sub_id:1, subject1:"Foo"},
{sub_id:2, subject1:"Foo2"}]};
angular.forEach($scope.subjects,function(subject){
var sub = angular.merge({checked:false}, subject);
$scope.tutorRequest.tutorsubject.push(sub);
});
$scope.allSubjectChecked = [];
$scope.updateChecked = function(){
console.log($scope.tutorRequest.tutorsubject);
$scope.allSubjectChecked = [];
angular.forEach($scope.tutorRequest.tutorsubject, function(sub){
if(sub.checked){
$scope.allSubjectChecked.push(sub);
}
});
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="sub in tutorRequest.tutorsubject" class="col-xs-12 col-sm-4">
<div class="chBoxPad" >
<input ng-model="tutorRequest.tutorsubject[$index].checked" type="checkbox" id="{{sub.sub_id}}" name="{{sub.subject1+'_'+$index}}" ng-change="updateChecked()"/>
<label for="{{sub.sub_id}}"><span></span>{{sub.subject1}}</label>
</div>
</div>
<h2> Subject selected : </h2>
<div ng-repeat="selected in allSubjectChecked">
<span ng-bind="selected.subject1"></span><br/>
</div>
</div>
Of course if this way of storing the data don't match to your backend; you can filter them when submitting the form. In order to not change the model displayed when doing that, perform this in the result of a var myData = angular.merge({},$scope.tutorRequest);
EDIT : change sample code by working snippet

TypeError: Cannot read property 'attempt1' of undefined

We have this code below that basically has ng-change that trigger to changeValue() to recalculate the sum up of each attempt1 to attempt9.
But has the following error on TypeError: Cannot read property 'attempt1' of undefined. I believe this something to do with child/parent $scope accessible ... but not sure how to solve this.
BTW ... we are utilising AngularJS and Ionic Framework.
.controller('RoutineActivityEditController', ['$scope', '$http', '$state', '$stateParams', '$localStorage', '$ionicPopup', function($scope, $http, $state, $stateParams, $localStorage, $ionicPopup) {
$scope.apiurl = $localStorage.apiurl;
$scope.options = [ {label: "Not Ok", value: 0},
{label: "ok", value: 1}
];
$http.get($scope.apiurl + '/routineactivity/' + $stateParams.uuId).success(function(data) {
$scope.routineActivityData = data;
$scope.total = $scope.routineActivityData.total;
$scope.target = $scope.routineActivityData.target;
// Debug JSON
//console.log('TEST2:' + data);
//$scope.showValues = JSON.stringify(data);
//console.log('TEST3:' + $scope.showValues);
$scope.changeValue = function ($scope)
{
$scope.total = $scope.routineActivity.attempt1.value;
//+ $scope.routineActivity.attempt2 +
// $scope.routineActivity.attempt3 + $scope.routineActivity.attempt4 +
// $scope.routineActivity.attempt5 + $scope.routineActivity.attempt6 +
// $scope.routineActivity.attempt7 + $scope.routineActivity.attempt8 +
// $scope.routineActivity.attempt9;
}
});
<form name="routineActivityForm" ng-submit="sendData()">
<div ng-if="routineActivityData.item.routine.maxnoofattempt == 10">
<div class="item item-input">Total: {{ total }} </div>
<div class="item item-input">Target: {{ target }}</div>
<label class="item item-input item-select">
<div class="input-label">Attempt #1</div>
<select name="attempt1" ng-model="routineActivity.attempt1" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt1=routineActivityData.attempt1">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #2</div>
<select name="attempt2" ng-model="routineActivity.attempt2" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt2=routineActivityData.attempt2">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #3</div>
<select name="attempt3" ng-model="routineActivity.attempt3" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt3=routineActivityData.attempt3">
</select>
<!--<select name="attempt3" ng-model="routineActivity.attempt3" ng-change="changeValue()">
<option ng-repeat="v in [0,1]" value="{{v}}" ng-selected="v==routineActivityData.attempt3">{{v}}</option>
</select>-->
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #4</div>
<select name="attempt4" ng-model="routineActivity.attempt4" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt4=routineActivityData.attempt4">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #5</div>
<select name="attempt5" ng-model="routineActivity.attempt5" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt5=routineActivityData.attempt5">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #6</div>
<select name="attempt6" ng-model="routineActivity.attempt6" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt6=routineActivityData.attempt6">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #7</div>
<select name="attempt7" ng-model="routineActivity.attempt7" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt7=routineActivityData.attempt7">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #8</div>
<select name="attempt8" ng-model="routineActivity.attempt8" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt8=routineActivityData.attempt8">
</select>
</label>
<label class="item item-input item-select">
<div class="input-label">Attempt #9</div>
<select name="attempt9" ng-model="routineActivity.attempt9" ng-change="changeValue()" ng-options="option.value as
option.value for option in options" ng-init="routineActivity.attempt9=routineActivityData.attempt9">
</select>
</label>
</div>
<button class="button button-full button-positive">Save</button>
<label >RESPONSE: {{ PostDataResponse }}</label>
</div>
</form>

Get value from ng-repeat key, value pair on POST Request

I am currently working on with a form that would get values from a json object. I'm done with it! (yey~) BUT when I POST the value I couldn't get anything. value="{{key}}" doesn't return a real data after POST. Any body can help me through solving this issue? Thanks alot!
<form class="form-horizontal" role="form" name="firewallForm" novalidate ng-init="firewallInit()">
<div class="form-group">
<label class="col-sm-1 control-label" for="firewallSource">Source</label>
<div class="col-sm-4 col-lg-2 col-md-2" ng-controller="entitiesController">
<select class="form-control input-medium" ng-model="firewallSource" ng-required="true">
<option value="" disabled selected> Enter Source Entity</option>
<option ng-repeat="(key, value) in entities.entities" value="{{key}}">{{key}}</option>
</select>
</div>
</div><div class="form-group">
<div class="col-sm-10">
<button type="button" class="btn btn-default" ng-click="submitFormFirewall()">
Submit
</button>
</div>
</div>
</form>
And my JS looks like:
$scope.submitFormFirewall = function() {
var postData = {};
postData["index"] = $scope.firewallRuleID;
postData["to"] = $scope.firewallDestination;
postData["application"] = $scope.firewallApplication;
postData["action"] = $scope.firewallAction;
postData["from"] = $scope.firewallSource;
Try this:
<select ng-model="firewallSource" ng-options="obj.key as obj.value for obj in entities.entities">
<option value=""> Enter Source Entity</option>
</select>
key and value are actually properties from your entities[0] object

Clone row of elements in AngularJS

I have this Angular form http://plnkr.co/edit/?p=streamer&s=ph0QHW513czywawl.
I need to clone the row on clicking ADD (+) and delete selected row on clicking DELETE (-).
Looking for a solution in AngularJS only. In current solution, the scopes are not working correctly. Also did not yet figure out how to implement (-) functionality.
index.html
<div ng-controller="MyCtrl" style="padding: 10px;">
<br/>
<div style="width: 90%; display: inline-block; border: 1px silver solid;">
<div class="row">
<div class="col-xs-3">
<select class="form-control" data-ng-model="hr.langauge.level" tooltip="Level">
<option value="Native" ng-selected="true">Native</option>
</select>
</div>
<div class="col-xs-4">
<select class="form-control" data-ng-model="hr.langauge.name" tooltip="Language">
<option value="">Language</option>
<option value="EN">English</option>
<option value="IT">Italian</option>
<option value="DE">German</option>
<option value="FR">French</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" data-ng-model="hr.langauge.remark" class="form-control" placeholder="Remark" tooltip="Remark">
</div>
<div class="col-xs-2">
</div>
</div>
<div select-last ng-repeat='item in items'></div>
</div>
<a class="btn" style="margin-bottom: 27px;" href="#" tooltip="Add" ng-click='addRow()'>
<i class="glyphicon glyphicon-plus"></i>
</a>
{{hr.langauge | json}}
language.html
<div class="row" style="padding-top: 5px;">
<div class="col-xs-3">
<select class="form-control" data-ng-model="hr.langauge.level" tooltip="Level">
<option value="">Level</option>
<option value="proficient">Proficient</option>
<option value="intermediate">Intermediate</option>
<option value="beginner">Beginner</option>
</select>
</div>
<div class="col-xs-4">
<select class="form-control" data-ng-model="hr.langauge.name" tooltip="Language">
<option value="">Language</option>
<option value="EN">English</option>
<option value="IT">Italian</option>
<option value="DE">German</option>
<option value="FR">French</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" data-ng-model="hr.langauge.remark" class="form-control" placeholder="Remark" tooltip="Remarks">
</div>
<div class="col-xs-2">
<a class="btn" href="#" tooltip="Delete" ng-click="deleteRow({{$index}});">
<i class="glyphicon glyphicon-minus"></i>
</a>{{$index}}
</div>
</div>
script.js
var myApp = angular.module('myApp',[]);
myApp.directive('selectLast', function () {
return {
restrict: 'A',
templateUrl: 'language.html'
}
});
function MyCtrl($scope) {
$scope.items = [];
$scope.newitem = '';
$scope.addRow = function(){
$scope.items.push($scope.newitem);
console.log('+ clicked');
}
$scope.deleteRow = function(rowNo) {
/*$scope.items.splice($scope.newitem);*/
console.log('- clicked in row ' + rowNo);
}
}
Based on your comment and plunker, it looks like you have the Add figured out. The remove row is easier. All you need to do is splice out the row. The issue on your view is you have the index surrounded by {{}} which isn't necessary.
$scope.deleteRow = function(rowNo) {
/*$scope.items.splice($scope.newitem);*/
$scope.languages.splice(rowNo, 1);
console.log('- clicked in row ' + rowNo);
};
<a class="btn" href="#" tooltip="Delete" ng-click="deleteRow($index);">
<i class="glyphicon glyphicon-minus"></i>
</a>{{$index}}
Here is an updated plunker: http://plnkr.co/edit/7vW24aDyZH02LO1iO7F3?p=preview&s=ph0QHW513czywawl
functionality is to have a click edit a row entry, create a temporary copy of the row data, then update.
Below link
https://thinkster.io/egghead/angular-copy/

Resources