Iam
routerApp.controller('HomePageCtrl',['$scope','$rootScope','searchFood','$timeout','$cookies',function($scope,$rootScope,searchFood,$timeout,$cookies){
var a=[];
$scope.fooditems= {};
$scope.Fn_LoadTimeslotWise = function(ts){
$scope.tsfooditems = {};
var longitude = $cookies.longitude;
var latitude = $cookies.latitude;
var sdate = $cookies.date;
var timeslot1 = ts;
var timeslot2 = timeslot1.substr(0,5);
var timeslot3 = timeslot1.substr(9,2);
var timeslot4 = timeslot2 + timeslot4;
var timeslot5 = timeslot1.substr(14,5);
var timeslot6 = timeslot1.substr(23,2);
var timeslot7 = timeslot5 + timeslot6;
var timefrom = sdate +" "+ timeslot2;
var timeto = sdate +" "+ timeslot5;
// console.log(timefrom);
//console.log(timeto);
$scope.tim = '.' + ts;
$timeout(function(){
searchFood.ChangeTimeSlot(latitude,longitude,sdate,timefrom,timeto).then(function(d){
$scope.tsfooditems ={};
$scope.tsfooditems = d[0];
});
});
}
}]);
<div ng-repeat = "timeslot in timeslots">
<h1 class="title"> {{ timeslot.date_time }} </h1>
<div class="row product-list-wrap" ng-init = "Fn_LoadTimeslotWise(timeslot.date_time)">
<h2 ng-show="showsearchmsg" ng-bind="searchresult"></h2>
<div class="col-md-4" ng-repeat="tsfooditem in tsfooditems ">
<div class="product-box view view-third">
<div class="product_img"> <span class="favourite"> <i class="fa fa-heart"> </i> </span>
<span class="veg"> <img src="images/veg.png"></span><img ng-src="{{tsfooditem.picture}}" width="200px" height="150px" ng-click="Fn_fooddetails(tsfooditem.menu_id)"> </div>
<div class="product-content">
<div class="product-title">
<h2 ng-bind="tsfooditem.menu_name | uppercase"></h2>
<h3 ng-bind="tsfooditem.chef_name"></h3>
</div>
<div class="product-rate"><b ng-bind="currency"></b>. <span ng-bind="tsfooditem.cost "></span> </div>
<div class="product-add"> <span class="bg"> - </span> <span ng-bind="tsfooditem.todays_menu_qty"></span> <span class="bg"> + </span> </div>
</div>
</div>
</div>
</div>
</div>
using angularjs version1.2.13.I need the below specified output
o/p
Timeslot 12.30-4.30pm
oil,
vegetable
Timeslot 1.30-3.30pm
oil,
meat
I am using two ng-repeat one inside another.But always the array displays only secondtime slot,.ie second overrides firstone .please help,Thanks in advance
It looks like your problem is that with your first ng-repeat, tsfooditems is not an array, it is an object literal. Ng-repeat will only work with arrays. Try changing
$scope.tsfooditems ={};
$scope.tsfooditems = d[0];
to
$scope.tsfooditems = [];
$scope.tsfooditems.push(d[0]);
Related
I'm starting with AngularJS and I am using a controller variable to navigate an array of questions, and it is working when using nextQuestion function, index gets updated and the next question is shown in the view, but if I try to obtain the same value (index) in a different function, it always returns 0.
I have seen on other questions that you should use an object to contain the variable to not manipulate primitive types directly in the controller, but it still does not work.
My controller:
myApp.controller('SurveyController',['$scope','$http', '$location','$routeParams','surveyMetrics','DataService',function($scope,$http, $location,$routeParams,surveyMetrics,DataService){
console.log('LOADED QUIZ CONTROLLER');
var vm = this;
vm.scope = {
index: 0
};
vm.surveyMetrics = surveyMetrics;
vm.surveyQuestions = DataService.surveyQuestions;
vm.DataService = DataService;
/*
vm.getQuestions = function(){
$http.get('/api/questions').then(function(response){
$scope.questions = response.data;
});
}
*/
/*
vm.activateSurvey = function(){
surveyMetrics.changeState(true);
}
*/
vm.getCurrentIndex = function(){
return vm.scope.index;
}
vm.nextQuestion = function () {
console.log('NEXT QUESTION!');
console.log('NUMBER OF QUESTIONS: '+ vm.surveyQuestions.length);
var currentIndex = vm.getCurrentIndex();
var newIndex = currentIndex+1;
scope = {};
if (currentIndex == vm.surveyQuestions.length) {
newIndex = vm.surveyQuestions.length -1;
}
vm.scope.index = newIndex;
console.log('Inside Object: '+vm.scope)
console.log('vm.index'+vm.scope.index);
console.log('vm.indexFunction'+vm.getCurrentIndex());
}
/*
vm.previousQuestion = function () {
console.log('PREVIOUS QUESTION!');
console.log('NUMBER OF QUESTIONS: '+ vm.surveyQuestions.length);
if (vm.scope.index == 0) {
vm.scope.index = 0;
}else{
vm.scope.index--;
}
}
*/
vm.activeSurveyQuestion = function(questionId,index){
console.log('question id and index',questionId,index);
if (questionId == index) {
var navBtn = document.getElementById('navBtn_'+index);
navBtn.classList.add('active');
}
}
vm.navigateSurvey = function () {
var answerPane = document.getElementById('answer-pane');
document.onkeydown = function (e) {
console.log('INSIDE KEYDOWN: ')
e.preventDefault();
var pressedKey = e.keyCode;
console.log('PRESSED KEY IN SURVEY: ' + pressedKey);
if (pressedKey === rightArrow) {
console.log('survey - right arrow pressed');
document.getElementById('nextQuestionBtn').click();
console.log('FUCKING INDEX FML!: '+vm.getCurrentIndex()+' | '+vm.scope.index);
var questionType = DataService.getQuestionType(vm.scope.index);
console.log('Survey Controller: question type: '+questionType);
}
if (pressedKey === leftArrow) {
console.log('survey - left arrow pressed');
document.getElementById('previousQuestionBtn').click();
}
(...)
My View:
<!--Satisfaction Survey-->
<div ng-controller="SurveyController as survey" ng-init="survey.getSurvey();">
<!--
<p ng-repeat="question in survey.surveyQuestions" ng-show ="survey.surveyMetrics.surveyActive">
{{question.question}}
</p>
-->
<!--Survey Modal -->
<div class="modal fade" id="surveyModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="text-center"> Customer Satisfaction Survey</div>
<div class="modal-header">
<h4 class="modal-title">{{survey.surveyQuestions[survey.getCurrentIndex()].question}}</h4>
</div>
<div class="modal-body survey" id="answer-pane">
<div class="row">
<div class="col-sm-2 survey-left-arrow" ng-click="survey.previousQuestion();" id="previousQuestionBtn">
<p>‹</p>
</div>
<div class="col-sm-8">
<!-- <p ng-repeat="answer in survey.surveyQuestions[survey.index].answers">{{answer}}</p> -->
<p ng-repeat="answer in survey.surveyQuestions[survey.getCurrentIndex()].answers">
<button type="button" class="btn" id="answerId_{{survey.getCurrentIndex()}}"
ng-class="{'survey-check-box': (survey.surveyQuestions[survey.getCurrentIndex()].type !== 'SingleChoice'),
'survey-btn_{{($index+1)}}': (survey.surveyQuestions[survey.getCurrentIndex()].type === 'SingleChoice')}">
<input type="checkbox" ng-if="survey.surveyQuestions[survey.getCurrentIndex()].type !== 'SingleChoice'"> {{answer}}
</button>
</p>
</div>
<div class="col-sm-2 survey-right-arrow " ng-click="survey.nextQuestion();" id="nextQuestionBtn">
<p>›</p>
</div>
</div>
</div>
<div class="text-center">
<strong> <p>Question: {{survey.surveyQuestions[survey.scope.index].questionNum}} of {{survey.surveyQuestions.length}}</p> </strong>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<!-- <nav aria-label="Survey navigation">
<ul class="pagination pagination-sm justify-content-center">
<div ng-repeat="question in survey.surveyQuestions" >
<li class="page-item">
<a class="page-link" id = "navBtn_$index" ng-click="survey.index = $index">{{question.id}}</a>
</li>
</div>
</ul>
</nav> -->
</div>
</div>
I would like for the controller to change the variable and the view to update accordingly
Thank you for your time and thank you in advance.
It's 'survey.scope.index' not 'survey.index' in your HTML. I think you may be unclear the difference between using 'this' and '$scope'. You're mixing the two together which is not necessary. I would suggest removing 'scope' and just reference it in your HTML as 'survey.index'.
I am having issue with the product list quantity buttons. I am new to angular js and trying to build product list which is having quantity increase and decrease button. I have build the list with ng-repeat, for increase and decrease button i am calling a function qty_incr() and qty_decr() it works but reflecting on every list item. I want to reflect only the same product. The same thing with jquery can done like this
$('.qty_incr').click(function(){
$(this).parents('.cart_product').children('#incr_decr_no').text(parseInt($(this).parents('.cart_product').children('#incr_decr_no').text() + 1));
})
Wondering how could same can achieve with angularjs, here is my code
<p ng-show="loading" class="loading"><img src="assets/img/Spinner-1s-200px.gif" width="50" /></p>
<div class="cart_products" ng-hide="loading" ng-repeat="product in data.products">
<div class="cart_product">
<div class="cart_pro_img"><img src="{{product.product_image}}" /></div>
<div class="cart_pro_detail">
<div class="cart_pro_title">{{product.product_name}}</div>
<div class="cart_pro_price">₹ {{product.price}}</div>
<div class="cart_pro_incr_decr">
<i class="fa fa-minus-circle" ng-click="qty_decr($parent.item)" style="color:#FF4646"></i>
<span id="incr_decr_no" ng-init="$parent.item.quantity=1">{{item.quantity}}</span>
<i class="fa fa-plus-circle" ng-click="qty_incr()" style="color:#68C549"></i>
<i class="fa fa-times-circle-o" {{item.product_id}}></i>
</div>
</div>
</div>
JS
app.controller("cart", function($scope, $http){
var guestid = localStorage.getItem('guestid');
$scope.loading = true;
$http.get(baseurl+"webservice.php?req=cartproducts&guestid="+guestid)
.then(function(response){
$scope.loading = false;
$scope.data = response.data;
//console.log($scope.data);
//$rootScope.cartcount(guestid);
});
$scope.qty_incr = function(item){
$scope.item.quantity = $scope.item.quantity + 1;
}
$scope.qty_decr = function(item){
if($scope.item.quantity > 1){
$scope.item.quantity = $scope.item.quantity - 1;
}
}
})
I have solved it by modifying the html and js code
My HTML
<div class="cart_products" ng-hide="loading" ng-repeat="product in data.products">
<div class="cart_product">
<div class="cart_pro_img"><img src="{{product.product_image}}" /></div>
<div class="cart_pro_detail">
<div class="cart_pro_title">{{product.product_name}}</div>
<div class="cart_pro_price">₹ {{product.price}}</div>
<div class="cart_pro_incr_decr">
<i class="fa fa-minus-circle" ng-click="qty_decr(item)" style="color:#FF4646"></i>
<span id="incr_decr_no" ng-init="item.quantity=1">{{item.quantity}}</span>
<i class="fa fa-plus-circle" ng-click="qty_incr(item)" style="color:#68C549"></i>
<i class="fa fa-times-circle-o" ng-click="removefromcart(product.product_id)"></i>
</div>
</div>
</div>
and JS is:
app.controller("cart", function($scope,$rootScope, $http){
var guestid = localStorage.getItem('guestid');
$http.get(baseurl+"webservice.php?req=cartproducts&guestid="+guestid)
.then(function(response){
$scope.data = response.data;
console.log($scope.data);
})
$scope.qty_incr = function(item){
item.quantity = item.quantity + 1;
}
$scope.qty_decr = function(item){
if(item.quantity > 1){
item.quantity = item.quantity - 1;
}
}
})
I wanted to create a custom directive addressing the scope to one of the object
JavaScript:
var module = angular.module('myApp', []);
module.controller('FoodCourtMenuController', function ($scope) {
$scope.GetMenu = function () {
var json = '[{"FoodCourtName":"Fiesta","FoodCourtDetails":[{"VendorName":"Adigas","VendorDetails":[{"FoodTypeName":"Breakfast","FoodTypeDetails":[{"Name":"Aloo paratha","Price":"Rs. 60","IsVeg":"True","ItemDetails":"2 nos"},{"Name":"North indian meal","Price":"Rs. 60","IsVeg":"True","ItemDetails":"sweet + roti"}]},{"FoodTypeName":"Lunch","FoodTypeDetails":[{"Name":"Dosa","Price":"Rs. 60","IsVeg":"True","ItemDetails":""},{"Name":"South indian meal","Price":"Rs. 80","IsVeg":"True","ItemDetails":"sweet + rice"}]}]},{"VendorName":"Kaamath","VendorDetails":[{"FoodTypeName":"Breakfast","FoodTypeDetails":[{"Name":"Sprouts","Price":"Rs. 60","IsVeg":"True","ItemDetails":"2 nos"},{"Name":"Fruit bowl","Price":"Rs. 60","IsVeg":"True","ItemDetails":"sweet + roti"}]},{"FoodTypeName":"Lunch","FoodTypeDetails":[{"Name":"Idly","Price":"Rs. 60","IsVeg":"True","ItemDetails":""},{"Name":"Palav","Price":"Rs. 80","IsVeg":"True","ItemDetails":"sweet + rice"}]}]}]},{"FoodCourtName":"Magna","FoodCourtDetails":[{"VendorName":"Polar Bear","VendorDetails":[{"FoodTypeName":"Breakfast","FoodTypeDetails":[{"Name":"Vanilla","Price":"Rs. 40","IsVeg":"True","ItemDetails":"2 nos"},{"Name":"Chocolate","Price":"Rs. 60","IsVeg":"True","ItemDetails":"3 nos"}]}]},{"VendorName":"Wonder Chicken","VendorDetails":[{"FoodTypeName":"Dinner","FoodTypeDetails":[{"Name":"Chicken","Price":"Rs. 100","IsVeg":"False","ItemDetails":"4 nos"}]}]}]}]';
var resultJSON = eval('(' + json + ')');
$scope.resultJSON = resultJSON;
}
});
module.directive('foodItem', function () {
var directive = {};
directive.restrict = 'AEC';
directive.template = '{{FoodCourtName}}';
directive.scope = {
FoodCourtName: "=FoodCourtName"
}
return directive;
});
HTML:
<div ng-controller="FoodCourtMenuController">
<input type="search" ng-model="txtSearch" name="txtSearch" placeholder="Search menu" />
<br />
<input type="button" ng-click="GetMenu()" value="Get Menu" />
<div id="menu" ng-repeat="x in resultJSON">
<foodItem FoodCourtName="{{x.FoodCourtName}}"></foodItem> <!--1st scenario-->
<span food-item food-court-name="{{x.FoodCourtName}}"></span> <!--2nd scenario-->
<food-item food-court-name="{{x.FoodCourtName}}"></food-item> <!--3rd scenario-->
</div>
</div>
I'm able to populate the data as below using ng-bind, I wanted to use the custom directive, so that i get a better understanding of the same.
Solution would be really appreciated. Thanks in advance.
<div id="menu" ng-repeat="foodCourt in resultJSON">
<div ng-repeat="vendor in foodCourt.FoodCourtDetails">
<div ng-repeat="foodType in vendor.VendorDetails">
<div ng-repeat="foodItem in foodType.FoodTypeDetails">
<div style="border-bottom:1px solid #000;">
<span ng-bind="foodCourt.FoodCourtName" style="padding:10px;"></span>
<span ng-bind="vendor.VendorName" style="padding:10px;"></span>
<span ng-bind="foodType.FoodTypeName" style="padding:10px;"></span>
<span ng-bind="foodItem.Name" style="padding:10px;"></span>
<span ng-bind="foodItem.Price" style="padding:10px;"></span>
</div>
</div>
</div>
</div>
</div>
In directive restrict you set E (element), so you have to use tag(element), not just an attribute.
After originally setting my controller weddingPrice value a change caused by a function does not seem to change the variable. Its probably a simple error- can anyone help?
When sendWeddingQuoteInfo() is called it seems to send the original weddingPrice, not the one which has been updated when the return journey toggle has been checked.
What should happen is that the wedding price should be set at the start through the local setup function. Then if the return journey toggle is switched to on, the returnJourneyChange() should be fired, changing the global weddingPrice. When the Submit Quote button is pressed this should take the updated weddingPrice and send it to the next page. Currently this does not happen- no idea why.
//wedding.html
<ion-view view-title="Wedding Pax Num" can-swipe-back="true">
<ion-content ng-controller="WeddingPaxCtrl">
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h2>CALCULATE WEDDING</h2>
</div>
</div>
<div class="padding20Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTFieldWithListItems" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<ion-toggle ng-model="returnJourney.checked" ng-change="returnJourneyChange()">Return Journey
</ion-toggle>
<ion-toggle ng-show="returnJourney.checked" ng-model="midnightReturn.checked" ng-change="midnightReturn()">Return Journey After Midnight</ion-toggle>
<div>
<h6 class="logText centerText"> {{ getCostBreakDown() }}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<a href="#/app/home/tester">
<button class="button button-full button-clear" ng-click="sendWeddingQuoteInfo()">Submit Quote</button>
</a>
</div>
</ion-view>
//controller.js
app.controller('WeddingPaxCtrl', function($scope, $stateParams, PassData, WhichQuote, CostOfMarriage, StoreQuoteData, WeddingData, DataThrow) {
var item = WeddingData.getWeddingDataObject();
$scope.passengerNo = 49;
$scope.returnJourney = { checked: false };
$scope.midnightReturn = { checked: false };
var weddingPrice;
$scope.returnJourney;
$scope.midnightReturn;
setup();
var sendInfo;
function setup() {
console.log("setup called");
weddingPrice = CostOfMarriage.getPrice(item[0], $scope.passengerNo, $scope.returnJourney.checked, $scope.midnightReturn.checked);
}
$scope.returnJourneyChange = function() {
console.log("return j called");
weddingPrice = 1000;
console.log("wedding price is now" + weddingPrice);
}
$scope.midnightReturn = function() {
}
$scope.getCostBreakDown = function() {
}
$scope.sendWeddingQuoteInfo = function() {
// var weddingPrice = $scope.weddingPrice;
console.log("WeddingPrice is " + weddingPrice + weddingPrice);
var sendInfo = ["Wedding Hire", item[0], $scope.passengerNo, "Extra", weddingPrice];
StoreQuoteData.setQuoteData(sendInfo);
WhichQuote.setInfoLabel("Wedding");
}
})
I think your ng-controller attribute is not at the right place. So the scope of your submit button is different.
I've moved the controller to ion-view element then the click is working as expected.
Please have a look at the demo below or here at jsfiddle.
(I've commented a lot of your code just to make the demo work.)
var app = angular.module('demoApp', ['ionic']);
app.controller('WeddingPaxCtrl', function($scope, $stateParams) { //, WeddingData, DataThrow) {
//var item = WeddingData.getWeddingDataObject();
$scope.passengerNo = 49;
$scope.returnJourney = {
checked: false
};
$scope.midnightReturn = {
checked: false
};
var weddingPrice;
$scope.returnJourney;
$scope.midnightReturn;
setup();
var sendInfo;
function setup() {
console.log("setup called");
weddingPrice = 100; //CostOfMarriage.getPrice(item[0], $scope.passengerNo, $scope.returnJourney.checked, $scope.midnightReturn.checked);
}
$scope.returnJourneyChange = function() {
console.log("return j called");
weddingPrice = 1000;
console.log("wedding price is now" + weddingPrice);
}
$scope.midnightReturn = function() {
}
$scope.getCostBreakDown = function() {
}
$scope.sendWeddingQuoteInfo = function() {
// var weddingPrice = $scope.weddingPrice;
console.log("WeddingPrice is " + weddingPrice + weddingPrice);
//var sendInfo = ["Wedding Hire", item[0], $scope.passengerNo, "Extra", weddingPrice];
//StoreQuoteData.setQuoteData(sendInfo);
//WhichQuote.setInfoLabel("Wedding");
}
})
<script src="http://code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
<link href="http://code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet" />
<ion-view ng-app="demoApp" view-title="Wedding Pax Num" can-swipe-back="true" ng-controller="WeddingPaxCtrl">
<ion-content>
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h2>CALCULATE WEDDING</h2>
</div>
</div>
<div class="padding20Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTFieldWithListItems" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<ion-toggle ng-model="returnJourney.checked" ng-change="returnJourneyChange()">Return Journey
</ion-toggle>
<ion-toggle ng-show="returnJourney.checked" ng-model="midnightReturn.checked" ng-change="midnightReturn()">Return Journey After Midnight</ion-toggle>
<div>
<h6 class="logText centerText"> {{ getCostBreakDown() }}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<!---<a href="#/app/home/tester">-->
<button class="button button-full button-clear" ng-click="sendWeddingQuoteInfo()">Submit Quote</button>
<!--</a>-->
</div>
</ion-view>
I'm having problems with receiving the value from an angular expression.
What should happen is when the endOfPageButton is clicked it should pass through the passengerNo and the title. The title is being passed through fine however the passengerNo is always undefined.
Can you tell me how I can get this to update as the passengerNo should be controlled by a textfield and I though the updated value should be automatically injected?
Thanks!
//mainquote.html
<ion-view view-title="{{title}}" can-swipe-back="true">
<ion-content ng-controller="QuoteItemCtrl">
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h6 class="headerTextPadding">REGION</h6>
<h2>{{title}}</h2>
</div>
</div>
<div class="padding40Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTField" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<div>
<h6 class="logText centerText">SCHOOLS > {{title}} > {{getCost()}} > # {{multiplier}}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<a href="#/app/home/finalquote">
<button class="button button-full button-clear" ng-click="sendQuoteInfo()">Submit Quote</button>
</a>
</div>
</ion-view>
//controller.js
app.controller('QuoteItemCtrl', function($scope, $rootScope, $stateParams, MakeSchoolQuote, DataThrow) {
var item = DataThrow.getDataObject();
$scope.passengerNo = 49;
$scope.title = item;
$scope.multiplier = MakeSchoolQuote.getMultipler(item);
$scope.getCost = function() {
$scope.pax = $scope.passengerNo;
var cost = $scope.multiplier * $scope.passengerNo
console.log("passenger numbers are" + $scope.pax);
return cost;
}
$scope.sendQuoteInfo = function() {
var data = [$scope.title, $scope.passengerNo, "N/A"];
var sendData = DataThrow.storeDataObject(data);
}
})