I am using ng-switch as follows. Is this the proper usage of ng-switch? I don't want it to be in an array, because I am using it with a scope variable.
In my HTML I have defined it like below. The userRole being the variable I want my search to be based on, and in the when I have used values based on what my div tag contents should work with.
<div ng-switch on ="userRole">
<div ng-switch-when="admin">
[...]
</div>
<div ng-switch-when="user">
[...]
</div>
I am not able to access the functionality of ng-switch, and my entire content part just vanishes.
In my .js file I have defined the following:
if(loginModel.authorities[0].authority=="ROLE_ADMIN"){
console.log("Me here");
$scope.userRole="admin";
}
else
if(loginModel.authorities[0].authority=="ROLE_USER"){
$scope.userRole="user";
}
else{
$scope.userRole="guest";
}
But my screen does not show anything, which makes me wonder how should I use ng-switch. I do not want to use ng-hide / ng-show.
Here is a neat fiddle which i found on how ng-switch works.... The way you are using ng-switch is perfectly valid. I am guessing your userRole holds no value which is making it fail, can you double check on this.
<div ng-controller="LoginCheckCtrl">
<div ng-switch on="loginData">
<div ng-switch-when="false" ng-include="'login'"></div>
<div ng-switch-when="true" ng-include="'logout'"></div>
</div>
</div>
<script type="text/ng-template" id="login">
<button ng-click="login()">Login</button>
</script>
<script type="text/ng-template" id="logout">
<button ng-click="logout()">Logout</button>
</script>
Controller code:
var app = angular.module('myApp', []);
function LoginCheckCtrl($scope) {
$scope.loginData = false
$scope.login = function() {
console.log($scope.loginData ? 'logged in' : 'not logged in');
$scope.loginData = true;
};
$scope.logout = function() {
console.log($scope.loginData ? 'logged in' : 'not logged in');
$scope.loginData = false;
};
}
Fiddle
https://jsfiddle.net/mrajcok/jNxyE/
Related
I have to hide a div on click .
<div ng-if="hideDiv">
<div>text text text text</div>
<a ng-click="hideMe(false)">Click Me To Hide</a>
</div>
Controller
this.hideMe = function(action){
$scope.hideDiv = action;
}
tested results are
console.log($scope.hideDiv) // Is false
{{hideDiv}} <!--Is false-->
But still ng-if doesn't hide div ?
Please, test the snippet below. I'm pretty sure your problem is due to some angularJS configuration failure
(function(){
angular.module("app", []).controller("testController", function($scope)
{
$scope.showDiv = true;
$scope.hideMe = function(IsVisible)
{
$scope.showDiv = IsVisible;
}
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="testController">
<div ng-if="showDiv">
<a ng-click="hideMe(false)">Click Me To Hide</a>
</div>
</div>
I have HTML code with inline element:
<span class="title" ng-click="update()">Rock</span>
How to replace this element on input element after click for edit?
And then after push enter on input return back span element?
I tried with directives ng-hide(), ng-show(). But I wonder
You can use either
<span class="title" ng-hide="isEdited" ng-click="update()">Rock</span>
or
<span class="title" ng-show="!isEdited" ng-click="update()">Rock</span>
or even
<span class="title" ng-if="!isEdited" ng-click="update()">Rock</span>
In any case you will want to reference something that can be truthy. For example in your controller you would have something like this in your function
/*the init function just makes sure that everything is setup
and nothing caries over from any local storage or anything
else you may be using*/
init();
init function(){
$scope.isEdited = false;
}
$scope.update = function(){
$scope.isEdited = true;
}
What you need to do is set a variable that contains the state;
<html>
<body ng-app="app">
<div ng-controller="mainController as $ctrl">
<span ng-if="!$ctrl.isInEditMode" class="title" ng-click="$ctrl.update()" ng-bind="$ctrl.spanText"></span>
<div ng-if="$ctrl.isInEditMode">
<input type="text" placeholder="Value for rock" ng-model="$ctrl.spanText" />
<button ng-click="$ctrl.update()">Done</button>
</div>
</body>
</html>
angular.module('app', [])
.controller('mainController', function($scope) {
this.isInEditMode = false;
this.spanText = 'Rock';
this.update = (function() {
this.isInEditMode = !this.isInEditMode;
}).bind(this);
});
I have prepared a Codepen that shows an possible solution: http://codepen.io/stefferd/pen/QdQrrv
Please see this jsFiddle
Within this fiddle I have two html divs that contain Ad Placements. I want to toggle them on/off using Angular so on one instance Ad 1 will show and in another instance Ad 2 will show instead. Note that in each div I have Javascript content that I want to also toggle on/off.
I essentially want to update the View.
HTML:
<div ng-controller="MyCntrl">
<div id = "ad1" class = "ad1">
AD 1
<img src ="http://images.dailytech.com/frontpage/fp__Apple-100x100.png"></img>
<script><!-- code here --></</script>
</div>
<div id = "ad2" class = "ad2">
AD 2
<img src ="http://fixmypod.ca/image/cache/data/NEWEST_Phones/Samsung_Galaxy_Note_2-100x100.jpg"></img>
<script><!-- code here --></</script>
</div>
</div>
Angular:
var app = angular.module('HelloApp', []);
app.controller('MyCtrl',['$scope','$element', function($scope, $element) {
$scope.changeView = function(ad){
//make ad x show and the other ad hidden
}
}]);
How can I go on doing this?
Simply use ng-show instead of a class and use a boolean property on your scope to trigger your divs.
HTML:
<div ng-controller="MyCntrl">
<div id="ad1" ng-show="toggle">
AD 1
<img src ="http://images.dailytech.com/frontpage/fp__Apple-100x100.png"></img>
<script><!-- code here --></</script>
</div>
<div id="ad2" ng-show="!toggle">
AD 2
<img src ="http://fixmypod.ca/image/cache/data/NEWEST_Phones/Samsung_Galaxy_Note_2-100x100.jpg"></img>
<script><!-- code here --></</script>
</div>
</div>
Angular:
var app = angular.module('HelloApp', []);
app.controller('MyCtrl',['$scope','$element', function($scope, $element) {
$scope.toggle = true;
$scope.changeView = function(ad){
$scope.toggle = !$scope.toggle;
}
}]);
Alternatively, you can use ng-include to only render a template based on a property.
HTML:
<div ng-controller="MyCntrl">
<div ng-if="condition">
<div ng-include="`template/path/ad1`"></div>
</div>
<div ng-if="!condition">
<div ng-include="`template/path/ad2`"></div>
</div>
</div>
You can do it by
ngShow
ngHide
ngIf
controller
$scope.myDiv=true;
view
<div ng-show="myDiv">
if true this will visible
</div>
I have a template in include. i am using that in 2 seperate instance in same page. how to i conditionally show the information within it?
code :
var myApp = angular.module("myApp", []);
myApp.controller("main", function($scope) {
$scope.firstTitle = true;
$scope.secondTitle = true;
});
html :
<div ng-controller="main">
<div class="show-first">
Your first info is :
<ng-include src="'info.html'"></ng-include>
//only need to show first tittle
</div>
<div class="show-second">
Your second info is:
<ng-include src="'info.html'"></ng-include>
//only need to show second tittle
</div>
</div>
<script type="text/ng-template" id="info.html">
<div>
<h1 ng-if="firstTitle">Info to show in first </h1>
<h1 ng-if="secondTitle">Infor to show in second </h1>
</div>
</script>
Live
If you want to show / hide segments of components based on a certain value/condition. You can follow this approach as well.
<div ng-show="isSectionShown()">
<h3>First Section Title</h3>
<div> Section Contents </div>
</div>
<div ng-show="isSectionShown()">
<h3>Second Section Title</h3>
<div> Section Contents </div>
</div>
angular.module('myApp', [])
.controller('MyController', function($scope) {
$scope.selectedSection = 'First';
$scope.isSectionShown = function() {
// This is for demonstration only, you can come with your implementation here to allow multiple checkings
return angular.equals($scope.selectedSection, 'First');
}
});
With similar conditional segments, you can use, ng-show, ng-hide, ng-if like statements.
Use two controllers separately to control the display of the template: Plunker.
.controller("oneCtrl", function($scope) {
$scope.firstTitle = true;
$scope.secondTitle = false;
}).controller("twoCtrl", function($scope) {
$scope.firstTitle = false;
$scope.secondTitle = true;
})
To fix this, you separate your templates.
<script type="text/ng-template id="info1.html">
<div>
<h1 ng-if="firstTitle">Info to show in first </h1>
</div>
</script>
<script type="text/ng-template id="info2.html">
<div>
<h1 ng-if="secondTitle">Info to show in second </h1>
</div>
</script>
The only other option is to access it form different controllers so you can turn one off and the other on. Separating the templates is the most versatile option IMO.
I am using ng-show and ng-hide to show the banner for my page. When I am logged in I wanna show different banner than for logged out. So I have two div with ng-show and ng-hide whose value I am changing in the controller to true and false.
Html code:
First banner:
<div id="firstPageHeader" class="navbar navbar-inverse" role="navigation" ng-show={{display}}>
Second banner:
<div id="restPageHeader" class="navbar-header" ng-hide={{display}}>
Controller:
var LoginController = function($scope){
$scope.display = true;
$scope.loginUser = function(credentials){
console.log(credentials.username);
$scope.display = false;
}
$scope.logout = function(){
console.log('logout');
$scope.display = true;
}
So here according to login and logout the display value will change to true and false but its not happening the value remains only true.
So I want to know why the value is not getting changed even I used $scope.$apply()
ngShow and ngHide directives expect expressions. So it should be without curly braces {{ (which are used for expression interpolation):
ng-show="display"
Can you explain how you are using loginUser() and logout() , coz in my case it is working fine..
My index.html is :
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="switchcases"> <div ng-controller="LoginController">
<div ng-click="logout()">Hi</div>
<div ng-click="loginUser()">Bye</div>
<hr>
<div ng-show="display">Hi</div>
<div ng-hide="display">Bye</div>
</div>
</body>
</html>
and app.js is :
angular.module('switchcases',[])
.controller('LoginController',function($scope){
$scope.display = true;
$scope.loginUser = function(credentials){
//console.log(credentials.username);
$scope.display = false;
};
$scope.logout = function(){
//console.log('logout');
$scope.display = true;
};
});
Cases :: When I click Hi(the one above <hr> tag) , only the 'Hi' below <hr> tag is displayed and when i click Bye(the one above <hr> tag),only the 'Bye' below <hr> tag is displayed , as expected .
The default value displayed will be 'Hi' below the <hr> tag.
Just check once that if you are using loginUser() inside the scope of the controller.
You just need to replace braces in ng-hide and ng-show double quotes
<div id="restPageHeader" class="navbar-header" ng-hide="display">
I am login
<img src="https://marketplace.canva.com/MABY09l0-Tw/1/0/thumbnail_large/canva-outlined-lettering-border-tumblr-banner-MABY09l0-Tw.jpg" alt="">
</div>
<div id="restPageHeader" class="navbar-header" ng-show="display">
I am logout
<img src="https://img.clipartfest.com/08e6b6a7495aca9bbc478fcf662cd29b_welcome-hanging-banner-welcome-banner-clipart_935-311.jpeg" alt="">
</div>
this is controller
$scope.display = true;
$scope.loginUser = function () {
console.log('loginUser');
$scope.display = false;
}
$scope.logout = function () {
console.log('logout');
$scope.display = true;
}