ng-if = 'false' is hiding the DIV - angularjs

I have the ng-click and ng-if directives on the same div.
Since the click variable is initially set to false the box DIV does not appear. If I set click to true or use !click for ng-if directive, then the box DIV appears but still no animation occurse upon clicking.
If I remove the ng-click from box DIV and add it to another DIV or button to do the toggle, animation works. However, the box DIV is still hidden initially and also disappears after the animation run.
HTML
<div ng-app="animationApp" ng-controller="c1">
<div ng-click="click=!click" ng-if="click" class="box"></div>
</div>
JS
angular.module('animationApp', ['ngAnimate'])
.controller('c1', function ($scope, $animate, $timeout) {
$scope.click = false;
})
;
CSS
.box {
width: 40px;
height: 40px;
background-color: indianred;
}
.box.ng-enter {
-webkit-transition: all 0.35s ease;
transition: all 0.35s ease;
transform: rotateZ(0deg);
}
.box.ng-enter-active {
transform: rotateZ(-180deg);
}
.box.ng-leave {
-webkit-transition: all 0.35s ease;
transition: all 0.35s ease;
transform: rotateZ(-180deg);
}
.box.ng-leave-active {
transform: rotateZ(0deg);
}
Demo
https://jsfiddle.net/ae8kwzxg/93/

This line is do nothing: "ng-click="click=!click".
The reason why is not work is because your $scope.click variable is primitive type (bool). It means in this line: ng-click="click=!click" click variable is a copy of your $scope.click variable and you just change the copy but not the actual $scope.click. If you will change your $scope.click variable to object, for example $scope.click = {active:true}
and change your HTML to: <div ng-click="click.active=!click.active" ng-if="click.active" class="box"></div>
It will work.
But better way is always do assignments in function:
<div ng-app="animationApp" ng-controller="c1">
<div ng-click="clicked()" ng-if="click" class="box"></div>
</div>
And your controller will be:
angular.module('animationApp', ['ngAnimate'])
.controller('c1', function ($scope, $animate, $timeout) {
$scope.click = true;
$scope.clicked = function(){
$scope.click = !$scope.click;
}
})
This is working example: https://jsfiddle.net/ae8kwzxg/94/

Related

how to add style in angularjs based on condition in html itself

I have the above page structure . i want to check a conditon to apply style in div with id if the buttonlayout has a class proceed.
<div>
<div id="main">
</div>
<div id='buttonlayout" class="proceed">
</div>
I am displaying the <div id='buttonlayout" class="proceed"></div> based on some conditions. so if the div doesnt exist I dont want to apply sty;e. How can I check this in angular js?
So the logic I am looking for is
if (.div-proceed) exist add an extra style to div (with id main). Is it possible to check in my html page itself (ng-class)?
You can set conditions in ng-class, as sample $scope.proceed default is false our condition is when $scope.proceed is true add class proceed to the element.
This mean if $scope.proceed = false the class not exist, and if it true class is exist.
var app = angular.module("app", []);
app.controller("ctrl", ["$scope", "$filter", function($scope, $filter) {
$scope.change = function() {
$scope.proceed = !$scope.proceed;
}
}]);
body {
padding-top: 50px;
}
a{cursor: pointer}
#buttonlayout {
width: 100px;
height: 100px;
transition: all 0.5s;
background-color: #ccc;
}
#buttonlayout.proceed {
width: 200px;
height: 200px;
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<a ng-click="change()">click to remove/add 'proceed' class</a>
<div id="buttonlayout" ng-class="{'proceed': proceed}"></div>
</div>

modal-content not setting height to contain content

I'm trying to learn Angular and modals just aren't working. The modal will show up, but the modal-content div isn't growing to contain all of the content. Instead, the content just runs out of the bottom of a very short modal. See picture below for what it is doing:
The three mini-paragraphs and the links below are supposed to be inside the modal.
Here is the controller for the "login" page:
MyApp.controller('loginController', ['$scope', '$location', '$translate', '$http', 'browserCheckService', 'modalService', function ($scope, $location, $translate, $http, browserCheckService, modalService) {
'use strict';
$scope.checkBrowser = function() {
if (browserCheckService.browserCheck() === false) {
try {
var settings = {
title: 'Incompatible Browser Warning',
size: 'lg',
templateUrl: 'Pages/browserReject.html',
controller: 'browserRejectController'
}
modalService.DisplayModal(settings);
} catch (err) {
alert(err.message);
}
}
};
$scope.checkBrowser();
}]);
Here is the controller for the browserReject:
MyApp.controller('browserRejectController', ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
'use strict';
$scope.close = function () {
$uibModalInstance.dismiss();
}
}]);
Here is the modalService:
angular.module('MyApp').service('modalService', ['$uibModal', function ($uibModal) {
'use strict';
this.DisplayModal = function (settings) {
if (settings === undefined) {
throw "Settings must be supplied for modal";
} else {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: settings.templateUrl,
controller: settings.controller,
size: settings.size
});
}
};
}]);
The browserCheckService is simply set to return to false so I could test the modal.
Here is the browserReject.html contents:
<div id="modalHeaderContainer">
<div id="title">Incompatible Browser Warning</div>
<div id="closeButton">
<button type="submit" class="close" ng-click="close()">X</button>
</div>
</div>
<hr />
<div id="jr_outer">
<div id=jr_inner">
<div id="jr_center">
<p translate={{'application__browser_reject_message'}}"></p>
<ul>
<li>
<a target="_blank" href="http://www.google.com/chrome/>Google Chrome</a>
</li>
<li>
<a target="_blank" href="https://www.mozilla.org/en-US/firefox/new/>Firefox</a>
</li>
<li>
<a target="_blank" href="http://www.apple.com/safari/>Safari</a>
</li>
<li>
<a target="_blank" href="http://windows.microsoft.com/en-us/internet-explorer/download-ie">Internet Explorer</a>
</li>
</ul>
</div>
</div>
</div>
Here is my CSS as it currently stands for the modal:
/* CSS for modal settings */
.ng-modal-overlay {
/* A dark translucent div that covers the whole screen */
position:absolute;
z-index:9999;
top:0;
left:0;
width:100%;
height:100%;
background-color:#000000;
opacity: 0.8;
}
.ng-modal-dialog {
/* A centered div above the overlay with a box shadow. */
z-index:10000;
position: absolute;
width: 50%; /* Default */
/* Center the dialog */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
background-color: #fff;
box-shadow: 4px 4px 80px #000;
}
.ng-modal-dialog-content {
padding:10px;
text-align: left;
}
.ng-modal-close {
position: absolute;
top: 3px;
right: 5px;
padding: 5px;
cursor: pointer;
font-size: 120%;
display: inline-block;
font-weight: bold;
font-family: 'arial', 'sans-serif';
}
#modalHeaderContainer {
width:100%;
height: 1.5em;
}
#title {
float:left;
font-size:1.7em;
padding-left:.5em;
}
#closeButton {
float:right;
padding-top:.5em;
padding-right:.5em;
}
/* End CSS for modal settings */
I've read the posts here and here, as well as several others and nothing seems to quite fit. I feel as though I'm overlooking something exceedingly simple, but can't find it. I've tried setting max-height on modal-content to 100%, but that doesn't change the display, either.
you should override the default .modal-window by doing something like the following in your css:
.mynewModal-modal-window .modal-window {
width: 100%;
height: 100%;
}
and add its' class as a parameter to the open method of $uibModal:
this.DisplayModal = function (settings) {
if (settings === undefined) {
throw "Settings must be supplied for modal";
} else {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: settings.templateUrl,
controller: settings.controller,
windowClass: 'myNewModal-modal-window',
size: settings.size
});
}
};
I just haven't found a way to override the modal-content yet via the controller :( (would be nice to have something similar)
Not exactly a fit but people looking for answers to uibmodal css overflow questions will need this:
Believe it or not, if you do not set your overflow:hidden or overflow:scroll on anything dynamic that you set a height to, like a repeater in angular, or injected content in jquery the content will simply look as if the modal isn't stretching, when in fact it is doing exactly as intended.

Programatically changing ngShow doesn't make use of ngAnimate classes

I'm trying to trigger ngShow via my controller, it works, but it doesn't make use of the ngAnimate classes that I need to get a fade transition.
It works like it should if I use a button to toggle ngShow, but not if I toggle it programmatically. Is this expected behavior? Can I get around it?
Plunk:
http://plnkr.co/edit/swJDP1KBBxcRfK9auYPs?p=preview
<body ng-controller="MainCtrl">
<input type="checkbox" ng-model="visible">
<div ng-show="visible" class="wrap" role="document">
Hello
</div>
</body>
var app = angular.module( "app", ['ngAnimate']);
app.run(function($rootScope) {
$rootScope.visible = false;
});
app.controller('MainCtrl', function($rootScope, $scope) {
$rootScope.visible = true;
});
.wrap.ng-hide-add-active {
display: block!important;
transition: 0.5s ease-in-out all;
}
.wrap.ng-hide-remove-active {
display: block!important;
transition: 0.5s ease-in-out all;
transition-delay: 0.5s;
}
.wrap.ng-hide {
opacity: 0;
}
You're run block and controller code likely get executed in the same digest cycle so Angular doesn't see the visible variable changing. This would work if you put your controller code in a timeout. e.g.
app.controller('MainCtrl', function($rootScope, $scope, $timeout) {
$timeout(function() {
$rootScope.visible = true;
});
});
http://plnkr.co/edit/5IhGE3ol5kI64OlT1e8v?p=preview

AngularJS animation makes hidden element appear when the page is loaded

I have a hidden component in a directive and yet when the page is loaded, the hidden element appears for the duration of the animation, which should only be triggered when the component's model is set to visible.
In this example I set the component to ng-hide="true" permanently, and when the page is loaded it still appears for half a second. In my real program the directive is much more complicated, so I placed the template in its own file, the problem doesn't appear if I just put it in a string.
I tried adding style="display:none" to the template's content, but then it doesn't react to model changes later.
<html ng-app="myApp">
<head>
<style>
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
transition: all linear 0.5s;
}
.overlay.ng-hide {
opacity: 0;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular-animate.min.js"></script>
<script>
angular.module("myApp", ['ngAnimate'])
.directive("overlay", function() {
return {
replace: true,
templateUrl: "overlay.html"
};
});
</script>
</head>
<body>
<overlay></overlay>
</body>
</html>
overlay.html:
<div class="overlay" ng-hide="true"></div>
Plunker: http://plnkr.co/edit/tYLkGwPJtFPxH2ES6qIe?p=preview
You could do the opposite using ng-class instead. Switch your CSS so the overlay only shows when you add a class show:
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
opacity: 0;
transition: all linear 0.5s;
}
.overlay.show {
opacity: 1;
}
Then just use the ng-class directive on your overlay, change the false value to whatever expression you have in mind.
<div class="overlay" ng-class="{show : false}"></div>
Here's the Forked Plunker
You can add a link function that adds a display: none property to the overlay element temporarily and then removed afterwards when the ng-hide directives kicks in by watching the ng-hide initial value and then removing the display: none property and deregistering the watcher.
Something like this:
FORKED PLUNKER
.directive("overlay", function() {
return {
templateUrl: "overlay.html",
link: function(scope, elem, attr) {
elem.css('display', 'none');
var dereg = scope.$watch(attr.ngHide, function(value) {
elem.css('display', '');
dereg();
});
}
};
});
Note that I have removed the replace property, it is depreciated in angular 3.0 and above. Reference

how to auto scroll to a div in angular js

i want my error message div to appear when i click on the submit button and i want that screen should auto scroll to that div my code snippet are below:
CSS:-
.error_msg
{
background: none repeat scroll 0 0 #F16200;
border: 0 solid #FF1055;
color: #EFEFEF;
display: block;
margin-top: 5px;
text-align: center;
width: 41%;
}
HTML of that div:
<div id="error" class="error_msg"> Speed Link Successfuly Created for {{display}}
controller which is invoking on submit:-
tmeModuleApp.controller('speedLinksController',function($scope,APIServices,Paths,$rootScope) {
$scope.setSpeedLink =function() {
var setLink = $('.active').attr('valset_link');
var display = $('.active').attr('valset_display');
var display_name = $('.active').attr('valset_link_name');
if(setLink != '')
{
$location.hash('error');
$scope.display= display_name;
}
}
}
Check out angulars $anchorScroll service.
You should inject $anchorScroll and call it after $locaiton.hash call.
Update
Example
HTML
<div id="scrollArea" ng-controller="ScrollCtrl">
<a ng-click="gotoBottom()">Go to bottom</a>
<a id="bottom"></a> You're at the bottom!
</div>
JS
function ScrollCtrl($scope, $location, $anchorScroll) {
$scope.gotoBottom = function (){
// set the location.hash to the id of
// the element you wish to scroll to.
$location.hash('bottom');
// call $anchorScroll()
$anchorScroll();
};
}

Resources