ng-show doesn't change on routeChange in AngularJS - html

I am developing an website with AngularJS and
mobile angular UI.
I have a menu bar which will show on clicking a button and should be closed when I click the options in it. But it isn't working as expected.
My files:
index.html
<div id="menuBar" ng-show="showMenu">
<div class="row">
page1
</div>
</div>
js for hiding menu
app.controller('mainController', function ($rootScope, $scope) {
$rootScope.showMenu = false;
$rootScope.$on("$routeChangeStart", function () {
$rootScope.showMenu = false;
});
});
So while switching to some other file the menu should hide ideally. But it didn't happen so.

The problem is how you show your menu. With this code:
<div ng-click="showMenu=!showMenu" class="btn btn-navbar"><i class="fa fa-bars"></i></div>
you change showMenu property of the child scope. This showMenu is not the same as $rootScope.showMenu. For this reason when you change $rootScope.showMenu in $routeChangeStart it doesn't effect child scope property. Instead you should do something like this:
<div ng-click="toggleMenuBar()" class="btn btn-navbar"><i class="fa fa-bars"></i></div>
and in controller:
$scope.toggleMenuBar = function() {
$rootScope.showMenu = !$rootScope.showMenu;
};

Related

Angularjs - Struggling to link a button to show the next component

I'm trying to figure out how to link a button to open a new HTML component but no matter which method I've tried I cannot get it to work
First I tried a JS Function:
function openNext(){
window.location = '../nextpage.html';
}
on this button code:
<div class="content">
<button type="button" ng-click="openNext()" class="nextBtn mat-raised-button"> Next!</button>
</div>
But that didn't do anything, so tried a simple href link, still nothing.
So I thought it was something perhaps with the routing
Notice that you are only asking to load a component on the click of a button. Nothing simpler:
<div class="content">
<button type="button" ng-click="openNext()" class="nextBtn mat-raised-button"> Next!</button>
<the-html-component-you-want-to-open
ng-if="isMyComponentOpen == true"
></the-html-component-you-want-to-open>
</div>
In your controller:
$scope.isMyComponentOpen = false;
$scope.openNext = function() {
$scope.isMyComponentOpen = true;
}
On the other hand, if you are looking into switching pages in your application, or loading external dialogs/modals containing other components, then you are asking the wrong question.

Angular JS - Modal not shown properly

I am learning to use Modal in AngularJS and Bootstrap 4 framework. I have manage to show it, but it is not shown properly, i.e. blocking components in background and cannot be shown if animation: true. I am not sure what causing this because I have inject ngAnimate and ui.bootstrap in my app controller.
Angular and ui-bootstrap version used
Angular v1.6.4
UI-bootstrap v2.5.0
Below I will provide my code.
View.html
<div class="container-fluid content-container" ng-app="listEmployee">
<div class="change-password-modal-container"></div>
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body" id="modal-body">
<ul>
<li ng-repeat="item in ctrl.items">
{{ item }}
</li>
</ul>
Selected: <b>{{ ctrl.selected.item }}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ctrl.ok()">OK</button>
<button class="btn btn-warning" type="button" ng-click="ctrl.cancel()">Cancel</button>
</div>
</script>
... <!--other html elements -->
</div>
Below is my code in controller which is related to showing the modal
Controller.js
var ctrl = this;
ctrl.items = ['item1', 'item2', 'item3'];
ctrl.animationsEnabled = false;
ctrl.open = function (size, parentSelector) {
var parentElem = parentSelector ?
angular.element($document[0].querySelector('.change-password-modal-container ' + parentSelector)) : undefined;
var modalInstance = $uibModal.open({
animation: ctrl.animationsEnabled,
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
controllerAs: 'ctrl',
size: size,
appendTo: parentElem,
resolve: {
items: function () {
return ctrl.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
ctrl.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
The z-index of my content is 1
mystyle.css
#content {
z-index: 1;
}
Below is the screenshot when the animation:false
Below is the screenshot when the animation:true (i.e. modal is not visible, it's like the modal is behind the screen)
Any help would be appreciated.
After reading this issue in GitHub, turns out this is the problem when using Bootstrap 4. Bootstrap 4 has different class names with Bootstrap 3, in this case, it is .in in version 3 become .show in version 4.
Following as suggested by IdanCo in the thread, which I rewrite below (so it will be easier for others to read) fixed this issue.
Change the class names from ui-bootstrap-tpls-###.js as below.
'class': 'modal-backdrop',
'ng-style': '{\'z-index\': 1040 + (index && 1 || 0) + index*10}',
'uib-modal-animation-class': 'fade',
'modal-in-class': 'in' //change this
'modal-in-class': 'show' //to this
});
if (modal.backdropClass) {
backdropDomEl.addClass(modal.backdropClass);
## -477,7 +477,7 ##
'ng-style': '{\'z-index\': 1050 + $$topModalIndex*10, display: \'block\'}',
'tabindex': -1,
'uib-modal-animation-class': 'fade',
'modal-in-class': 'in' //change this
'modal-in-class': 'show' //to this
}).append(content);
if (modal.windowClass) {
angularDomEl.addClass(modal.windowClass);
Hope this update could help someone in the future.

angular-ui modal is not initially hidden

I'm following this angular recipes page for adding a modal dialog to my ui. It suggests the following markup, which I've added to one of my views.
... html for my view is here ...
<button class="btn" ng-click="open()">Open Modal</button>
<div modal="showModal" close="cancel()">
<div class="modal-header">
<h4>Modal Dialog</h4>
... etc, from the recipe doc
</div>
What I want to see is my view, plus an "Open Modal" button on the bottom and nothing else. What I see instead is the button and the content of the modal already visible on the page.
The very next words in the recipe doc are:
Note that even though we don’t specify it explicitly the modal dialog
is hidden initially via the modal attribute. The controller only
handles the button click and the showModal value used by the modal
attribute.
Why is my modal mark up initially visible on the page? I think I have installed angular-ui properly... in my index.html:
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
And in my app JS:
angular.module('MonteAdmin', [
...
'ui.bootstrap',
...
])
That recipes page is likely out of date. At the time of the writing it might have been possible to pass a variable showModal to the modal directive to reveal or hide it. In your controller, you would have been able to show the modal by setting the scope variable showModal to true or false:
$scope.showModal = false;
$scope.open = function() {
$scope.showModal = true;
}
The current version does not work that way. You will have much better experience if you read the official documentation for the library at Angular UI Bootstrap
If you are using the latest version of the library, the directive is no longer modal but uib-modal. In addition, you have a bit more work to do to implement your modal.
Modal markup should be in a script tag, with a type set to text/ng-template as per the official example:
<script type="text/ng-template" id="stackedModal.html">
<div class="modal-header">
<h3 class="modal-title" id="modal-title-{{name}}">The {{name}} modal!</h3>
</div>
<div class="modal-body" id="modal-body-{{name}}">
Having multiple modals open at once is probably bad UX but it's technically possible.
</div>
</script>
To actually open the modal, your button click should trigger the following example function:
var modalInstance = $uibModal.open({
animation: $ctrl.animationsEnabled,
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
controllerAs: '$ctrl',
size: size,
appendTo: parentElem,
resolve: {
items: function () {
return $ctrl.items;
}
}
});
You must also define a controller for the modal, itself:
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items) {
var $ctrl = this;
$ctrl.items = items;
$ctrl.selected = {
item: $ctrl.items[0]
};
$ctrl.ok = function () {
$uibModalInstance.close($ctrl.selected.item);
};
$ctrl.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
All of this code is found on the official documentation for Angular UI Bootstrap

Cannot create a button in a draggable list

I have an ng-nestable div and inside that there is a simple button that has an ng-click event hooked up to it. When I click on that button it doesn't register the click but rather starts the drag and drop process.
Here is my HTML Code,
<div class="col-sm-1 text-right"><button ng-click="fnEditHomeSlider({{$item.SliderID}})" class="btn btn-blue btn-sm" nestable-button><i class="fa fa-edit"></i> Edit</button>
</div>
Note: Is your code something like, try it, Hope it could also matches your need or give us your code
1.) Create a custom directive
myApp.directive('nestableButton', function() {
return {
restrict: 'A',
link: function(scope, element) {
$(element).on("mousedown", function(e) {
e.preventDefault();
return false;
});
$(element).on("click", function(e) {
e.preventDefault();
window.location = $(this).attr("href"); //if you want your URL to update
return false;
});
}
};
});
2.) In HTML
</i>
</i>
Lokesh Kakran is right about this.
And here is live demo project about your solution.
demo : http://pathik.linedeer.com/ng-nestable/
code : https://github.com/pathikdevani/ng-nestable/blob/gh-pages/index.html

HTML Back button that disappears after clicking it once?

i need a Back Button that disappears, looked at ngdisabled from AngularJS but quite the beginner.
<button onClick="history.go(-1);" >Go back</button>
Already got the back button, and i want to disable it after its been clicked?
Use
<button onClick="history.go(-1);this.disabled=true;" >Go back</button>
Assuming your button is contained by an Angular-Controller, a more Angular-approach to this problem would be something like this:
<div ng-controller="controller as ctrl">
<button ng-click="ctrl.GoBackInHistory()"
ng-disabled="ctrl.isBackButtonDisabled">
Go back
</button>
</div>
<script>
angular.module('app', [])
.controller('controller', controller);
controller.$inject = ['$window'];
function controller($window) {
var vm = this;
vm.isBackButtonDisabled = false;
vm.GoBackInHistory = GoBackInHistory;
function GoBackInHistory() {
$window.history.back(); //or $window.history.go(-1);
vm.isBackButtonDisabled = true;
}
}
</script>
Please try this.
<button ng-click="goBack" ng-disabled="backDisabled">Go back</button>
click function:
$scope.goBack = function(){
$window.history.back();
$scope.backDisabled = true; // use $rootScope to access everywhere of app.
}