Ionic: $scope change not reflected on the page - angularjs

On my Ionic App, I have a view that has a button. When you click on the button, a list of player would be served in a modal, when clicked on a player name, the name of the selected player should be shown on the page and the button should be hidden.
Here's the codepen for the same - http://codepen.io/mradul/pen/vNywqG
However, when I select a value(player name) on the modal, the scope variable gets updated(as it's printed on the console), but the change is not reflected on the page.
HTML -
<ion-header-bar class="bar-positive bar-header">
<h1 class="title">Selected Player</h1>
</ion-header-bar>
<ion-content>
<script id="select-player.html" type="text/ng-template">
<div class="modal">
<ion-header-bar>
<h1 class="title">Choose Player</h1>
</ion-header-bar>
<ion-content>
<ion-list>
<ion-item ng-repeat="player in getPlayerList().players" ng-click="closeModal(player)">
{{player}}
</ion-item>
</ion-list>
</ion-content>
</div>
</script>
<br/><br/>
<div id="player">
{{sc.player}}
<div class="row">
<a class="button" ng-click="openModal()" ng-show="sc.player.playerName == null">Set player</a>
<div ng-hide="sc.player.playerName == null">
<div class="col col-33 " > {{sc.player.playerName}}</div>
</div>
</ion-content>
JS -
angular.module('ionicApp',['ionic']).controller('scoringController', function ($scope, $ionicModal) {
$scope.player = {playerName:""};
$scope.getPlayerList = function () {
console.log("getting player list");
return {
"players": [
"Mradul",
"JAin",
"Phunsuk Wangdu",
"Hulk"
]
};
};
$ionicModal.fromTemplateUrl('select-player.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal
})
$scope.openModal = function () {
console.log("Setting model open ");
$scope.modal.show()
}
$scope.closeModal = function (playerName) {
$scope.player.playerName = playerName;
//$scope.activePlayers[0] = playerName;
console.log("Player Set ", $scope);
console.log("model closed");
$scope.modal.hide();
};
$scope.$on('$destroy', function () {
$scope.modal.remove();
});
});
Player was initially a primitive(string), but I changed it into an object by having a property - playerName. May be I didn't do it right. Please suggest how can I have the parent scope variable updated correctly. I might have missed something very basic.

Write $scope.$apply() after your change

Try this, Am not sure about it
{{player.playerName}}

Related

Ionic - How to create a border around my button when being click?

Currently I am trying to make my mobile app to be like, a default border around my button "OFF" when i go in my app. After "ON" button is being click, the border will be around my "ON" button, When i click "OFF" button, the border will change back to be around my "OFF" button.
HTML:
<ion-view title="Single Projector Room" id="page9">
<ion-nav-buttons side="left">
<button class="button back-button buttons button-clear header-item" ng-click="goBack()">
<i class="icon ion-ios-arrow-back"> Back</i>
</button>
</ion-nav-buttons>
<ion-content padding="true" style="background: url(img/EDD79InSMCeYeMOSchjm_remote.jpg) no-repeat center;background-size:cover;" class="has-header">
<div class="row">
<div class="col"><br><br><br><br></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><h4 style="color: #FFFFFF;">Single Projector Control</h4></div>
<div class="col col-30 text-center"><h4 style="color: #FFFFFF;">Screen Control</h4></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><button class="button button-balanced button-large icon-right ion-power">On</button></div>
<div class="col col-30 text-center"><button class="button button-dark button-large icon-right ion-arrow-up-c">Up</button></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><button class="button button-assertive button-large icon-right ion-android-cancel">Off</button></div>
<div class="col col-30 text-center"><button class="button button-dark button-large icon-right ion-arrow-down-c">Down</button></div>
</div>
<div class="row">
<div class="col"><br><br></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><h4 style="color: #FFFFFF;">Mic Control</h4></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><button class="button button-balanced button-large icon-right ion-power">On</button></div>
</div>
<div class="row">
<div class="col col-offset-5 col-30 text-center"><button class="button button-assertive button-large icon-right ion-android-cancel">Off</button></div>
</div>
</div>
</ion-content>
</ion-view>
controller.js:
angular.module('app.controllers', [])
.controller("remoteCtrl", function($scope, $stateParams, $state, $ionicHistory) {
$scope.single = function() {
$state.go("singleProjectorRoom");
}
$scope.dual = function() {
$state.go("dualProjectorRoom");
}
$scope.myGoBack = function() {
$ionicHistory.goBack();
}
})
.controller('menuCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {
}])
.controller('loginCtrl', function($scope, $rootScope, $ionicHistory, sharedUtils, $state, $ionicSideMenuDelegate) {
$rootScope.extras = false; // For hiding the side bar and nav icon
// When the user logs out and reaches login page,
// we clear all the history and cache to prevent back link
$scope.$on('$ionicView.enter', function(ev) {
if(ev.targetScope !== $scope){
$ionicHistory.clearHistory();
$ionicHistory.clearCache();
}
});
//Check if user already logged in
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
//Removes back link to login page
$ionicHistory.nextViewOptions({
historyRoot: true
});
$ionicSideMenuDelegate.canDragContent(true); // Sets up the sideMenu dragable
$rootScope.extras = true;
sharedUtils.hideLoading();
$state.go('tabs.home', {}, {location: "replace"});
}
});
$scope.loginEmail = function(formName,cred) {
if(formName.$valid) { // Check if the form data is valid or not
sharedUtils.showLoading(); //starts the loading popup
//Email Login via Firebase
firebase.auth().signInWithEmailAndPassword(cred.email,cred.password).then(function(result) {
// You dont need to save the users session in your local session or cookies. Firebase handles it.
// You only need to :
// 1. clear the login page history from the history stack so that you cant come back
// 2. Set rootScope.extra;
// 3. Turn off the loading
// 4. Got to menu page
$ionicHistory.nextViewOptions({
historyRoot: true //1
});
$rootScope.extras = true; //2
sharedUtils.hideLoading(); //3
$state.go('tabs.home', {}, {location: "replace"}); //4
},
function(error) {
sharedUtils.hideLoading();
sharedUtils.showAlert("ERROR!","Authentication Error");
}
);
}else{
sharedUtils.showAlert("ERROR!","Enter in email format");
}
};
})
.controller("logoutCtrl", function($scope, $state, $ionicPopup, $rootScope, sharedUtils, $ionicHistory,$ionicSideMenuDelegate, intentService, errorMessageService) {
$scope.logoutNo = function() {
intentService.setloginFlagTrue();
$state.go("tabs.home");
console.log("Logout is reverted.The flag is now set as " + intentService.getloginFlagStatus());
}
$scope.logoutYes = function() {
sharedUtils.showLoading();
// Main Firebase logout
firebase.auth().signOut().then(function() {
errorMessageService.write("Thank you","You have logout successfully.");
$ionicSideMenuDelegate.canDragContent(false); // To remove the sidemenu white space
$ionicHistory.nextViewOptions({
historyRoot: true
});
$rootScope.extras = false;
sharedUtils.hideLoading();
$state.go('login', {}, {location: "replace"});
}, function(error) {
sharedUtils.showAlert("Error","Logout Failed")
});
}
})
.controller('singleProjectorRoomCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {
$scope.goBack = function() {
window.history.back();
}
}])
.controller('dualProjectorRoomCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {
$scope.goBack = function() {
window.history.back();
}
}])
.controller('homeCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {
}])

button toggle not working with angularjs

I'm trying to simply change the button text with a toggle but I'm failing to do so I don't know why.The button only shows 'ver fotos' and doesnt change to my 2nd option. Maybe i can't put two ng-click like that? This is my code:
angular.module('app.controllers', [])
.controller('perfilCtrl', ['$scope', '$stateParams', "detailService",// The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams, detailService) {
$scope.detalhes=detailService.data;
$scope.goEsconder = false;
$scope.toggle = true;
$scope.$watch('toggle', function(){
$scope.toggleText = $scope.toggle ? 'Ver fotos' : 'Ver características';
})
}])
<ion-view id="page4">
<ion-content padding="true" class="backg">
<div class="row">
<div class="col">
<h3 class="name">{{detalhes[0]}}</h3>
</div>
</div>
<div class="row">
<div class="col">
<p class="descricao">{{detalhes[2]}}</p>
</div>
</div>
<div class="row">
<div class="col text-center" ui-sref="contacto"><button class="button b2">
Contactar
</button>
</div>
<div class="col text-center"><button class="button b3" ng-click="goEsconder = !goEsconder; toggle = !toggle">
{{toggleText}}
</button>
</div>
</div>
</ion-content>
</ion-view>
Use an object so that child scopes will use inheritance. There is no inheritance on primitives in javascript.
So what happens is you are only changing the primitive in the child scope and that change is hidden from the parent so the watch won't fire
$scope.myModel = { toggle: true };
$scope.$watch('myModel.toggle', function(nVal, oVal) {
$scope.toggleText = $scope.myModel.toggle ? 'Ver fotos' : 'Ver características';
});
HTML
<button class="button b3" ng-click="myModel.toggle = !myModel.toggle">
{{toggleText}}
</button>
DEMO
I believe it works fine: http://jsfiddle.net/Lvc0u55v/10594/
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.toggleText = 'click me'
$scope.toggle = true
$scope.$watch('toggle', function(){
$scope.toggleText = $scope.toggle ? 'Ver fotos' : 'Ver características';
})
}
Html:
<div ng-controller="MyCtrl">
<div class="col text-center">
<button class="button b3" ng-click="toggle = !toggle">{{toggleText}}</button>
</div>
</div>
Change your watcher to use the true parameter, this way:
$scope.$watch('toggle', function(toggle){
$scope.toggleText = toggle ? 'Ver fotos' : 'Ver características';
}, true)
this will make your code work because watchers doesn't work natively on boolean values (because boolean values are passed by value on javascript: for your watcher toggle is always true )
Specifying the true parameter, however, will change your watcher to a deep watcher, being so able to catch value changes and not only references changes.

ionic modal scope not working using $ionicModal.fromTemplateUrl()

I am trying to open an ionic modal just after logging in to an app. The modal is opening fine along with the data. The problem is when i attaching ng-click event corrosponding to OK or CANCEL button, it is not working.The function is not called.Maybe there is a problem with the scope. I cant figure out why..
This is the controller
$scope.dummyData = NetworkDisplay.getHospitals();
console.log($scope.dummyData.inviteToNetwork);
if ($scope.dummyData.inviteToNetwork) {
$ionicModal.fromTemplateUrl('templates/network/modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
console.log($scope.dummyData);
$scope.ok = function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].checked === true)
HospitalsNetwork.set(data[i]);
// console.log(data[i].checked);
}
$scope.modal.remove();
$state.go('tab.networks');
};
$scope.cancel = function() {
$scope.modal.remove();
$state.go('tab.dashboard');
};
This is my modal.html
<ion-view>
<ion-content>
<div class="card">
<p class="padding">Do you like the following hospitals to be added to your network?</p>
<!--<ul class="list">
<li class="item item-checkbox" ng-repeat="hospital in dummyData.inviteToNetwork">
<label class="checkbox">
<input type="checkbox">
</label>
Flux Capacitor
</li>
</ul>-->
<div class="list">
<ion-checkbox ng-repeat="hospital in dummyData.inviteToNetwork" ng-model="hospital.checked">
{{hospital.name}}
</ion-checkbox>
</div>
<!-- <div class="modal-footer"> -->
<button type="button" ng-click="ok(dummyData.inviteToNetwork)">OK</button>
<button type="button" ng-click="cancel()">Cancel</button>
<!-- </div> -->
</div>
</ion-content>
</ion-view>
I was stuck in the same and finally figured out that it is not working on Ionic app but it is working on IOS emulator.
So finally you can't able to test Ionic modal on Ionic app till the Ionic team will fix this issue.

Image popup function is working but unabel to see the image on pop-up

1.I was building a ionic Mobile App in that i have a gallery and i was trying to view a image as pop-up from gallery, pop-up was
working fine and it fetch the image path too but unable to see image
on pop-up.
here my app.js
function galleryController($scope, $http, $rootScope, $location,$ionicModal, profile){
/*alert(0);*/
profile.galleries(window.localStorage['token']).success(function(data){
/*alert(JSON.stringify(data));*/
if(data.status == 200)
{
$scope.gallery = data.data.gellary;
//alert(JSON.stringify(data));
}
$ionicModal.fromTemplateUrl('partials/gallery.html', function(modal) {
$scope.gridModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// open video modal
$scope.openModal = function(selected) {
$scope.data = selected;
$scope.gridModal.show();
};
// close video modal
$scope.closeModal = function() {
$scope.gridModal.hide();
};
//Cleanup the video modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.gridModal.remove();
});
});
};
kangaroo.directive('gridImage', function(){
alert("yyyyyyyyyyyyyyyyyyy");
return function($scope, element, attrs){
var url = attrs.gridImage;
element.css({
'background-image': 'url(' + url +')',
});
};
});
I was fetching image from s3 Amazon by using api , this is html
code in my gallery.html where i get the data from my controller i
can see the image in gallery and not able to see on pop-up don't
know what was the problem.
**gallery.html page**
<ion-header class="bar bar-header bar-balanced">
<!-- <button class="button button-icon icon ion-navicon"></button> -->
</ion-header>
<div class="padding" >
<div ng-controller="galleryController">
<ion-content class="content has-header ionic-pseudo" style="margin-top:19px">
<div ng-repeat="x in gallery" >
<img ng-src="{{x.thumb_img}}" ng-click="openModal(x)" alt="" class="gallery_i" />
</div>
</ion-content>
<script id="partials/gallery.html" type="text/ng-template">
<div class="modal" ng-click="closeModal()">
<img ng-src="{{data.thumb_img}}" />
</div>
</script>
</div>
</div>
please help me out of this
Thanks in advance.

ng-show doesn't reveal instantly but on page change?

I have this code to show loading message.
<ion-view title="Rooms">
<ion-content>
<ion-list ng-show="rooms">
<ion-item class="item-icon-left" ng-repeat="room in rooms" type="item-text-wrap" ng-click="openChatRoom(room.id)">
<i class="icon {{room.icon}}"></i>
<h2>{{room.name}}</h2>
</ion-item>
</ion-list>
<ion-list ng-hide="rooms.length">
<ion-item class="textCenter">
<i class="icon ion-loading-c"></i> Loading Rooms
</ion-item>
</ion-list>
</ion-content>
What it does now is just showing "loading Rooms" for awhile and nothing shows up.
But when I change page and return to this page again, items in ng-show just shows up and never show "loading Rooms" again.
So why this ng-show doesn't reveal instantly? Below are its controllers
.controller('RoomsCtrl', function($scope, Rooms, Chats, $state, $ionicModal) {
//console.log("Rooms Controller initialized");
$scope.rooms = Rooms.all();
console.log($scope.rooms);
$scope.openChatRoom = function (roomId) {
$state.go('app.chat', {
roomId: roomId
});
};
})
.factory('Rooms', function ($firebase) {
// Might use a resource here that returns a JSON array
var ref = new Firebase(firebaseUrl);
var rooms = $firebase(ref.child('rooms')).$asArray();
return {
all: function () {
return rooms;
},
get: function (roomId) {
// Simple index lookup
return rooms.$getRecord(roomId);
}
}
});

Resources