how do i add css style in angularJs? - javascript

<ul class="nav nav-pills tabscount">
<li ng-repeat="section in submissionCtlr.submissionForm.sections" ng-class="{ active:submissionCtlr.isSelected(section.sectionId) }" > {{section.sectionName}}</li>
</ul>
<input type="submit" value="Submit" style="display:none" class="btn btn-primary" ng-click="submissionCtlr.submitForm()" ng-disabled="submitForm.$invalid">
this.tab=1;
this.selectTab = function(setTab){
var tabcount = $scope.submissionCtlr.submissionForm.sections.length;
if(this.tab != tabcount){
this.tab++;
}else {
}
On click next button i got count, at the end of li count need to submit. submit should be display and next should be display: none. how can i acheive in anugularJs.

You can use ng-show to tell that if the current tab(tab) is the same as the total number of tabs(submissionCtlr.submissionForm.sections.length).
<input type="submit" value="Submit" ng-show="submissionCtlr.tab==submissionCtlr.submissionForm.sections.length" class="btn btn-primary" ng-click="submissionCtlr.submitForm()" ng-disabled="submitForm.$invalid">

For modifying the DOM elements in angularjs. The proper way is to create a directive and access the element in that directive.
For example -
<button class="btn btn-primary" ng-click="submit()" on-submit> Submit </button>
JS:
angular.module('myApp')
.directive('onSubmit', ,function() {
return {
restrict: 'A',
link: function($scope,el) {
$scope.submit = function() {
el[0].css("display", "none"); //change the css here accordingly.
}
}
};
});
You can also add the class like
element.addClass('myclass');

Related

Find out if Twitter Bootstrap checkbox button has been clicked

I am new to AngularJS and could really use some help. I have a button that's shown below that is part of a form. I need to show a modal when the form is submitted if the button is not clicked. How can I perform this check? I have tried several things with no luck.
<button ng-repeat="car in cars" btn-checkbox-false
class="btn btn-default btn-block text-left"
ng-click="AddRemoveCar(cars)">
<i ng-show="carInStock(cars)"
class="fa fa-check pull-right btn-success btn btn-xs" />
<i ng-show="!carInStock(cars)"
class="fa fa-plus pull-right btn-warning btn btn-xs" />
{{car.Model}}
</button>
Consider using the UI-Bootstrap uib-btn-checkbox directive for your Twitter Bootstrap checkboxes.
The uib-btn-checkbox directive, makes a group of Twitter Bootstrap buttons behave like a set of checkboxes.
Then your submit function can check the state of the model as bound with the ng-model directive.
For more information, see
UI-Bootstrap API Reference - Buttons
The DEMO1
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap'])
.controller('ButtonsCtrl', function ($scope) {
$scope.checkModel = {
left: false,
middle: true,
right: false
};
$scope.$watchCollection('checkModel', function () {
$scope.checkResults = [];
angular.forEach($scope.checkModel, function (value, key) {
if (value) {
$scope.checkResults.push(key);
}
});
});
})
<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/angular-animate/angular-animate.js"></script>
<script src="//unpkg.com/angular-sanitize/angular-sanitize.js"></script>
<script src="//unpkg.com/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js"></script>
<link href="//unpkg.com/bootstrap#3/dist/css/bootstrap.min.css" rel="stylesheet">
<body ng-app="ui.bootstrap.demo" ng-controller="ButtonsCtrl">
<h4>Checkbox</h4>
<pre>Model: {{checkModel}}</pre>
<pre>Results: {{checkResults}}</pre>
<div class="btn-group">
<label class="btn btn-primary" ng-model="checkModel.left"
uib-btn-checkbox>Left</label>
<label class="btn btn-primary" ng-model="checkModel.middle"
uib-btn-checkbox>Middle</label>
<label class="btn btn-primary" ng-model="checkModel.right"
uib-btn-checkbox>Right</label>
</div>
</body>
I would be setting up some flag on the button click and check that value on form submit.
In my controller define a variable like btnClickedFlag = false;
In Button click function:
AddRemoveCar(cars) => {
this.btnClickedFlag = true;
}
Now on form submit , you can just check if btnClickedFlag is true if not display your modal/dialogue/overlay on screen.

How can I call a javascript directive link function from a typescript controller l [duplicate]

This question already has answers here:
How to call a method defined in an AngularJS directive?
(13 answers)
Closed 4 years ago.
Someome, please tell me how can I access a javascript directive link function from a typedScript controller. I have a button outside the directive and I want to call that function in my pageController when my user button is clicked. I´ve tried several tutorials but i can´t have it working.
1-This is an extract of th directive.js file
I want to call the function from my controller
directive.js - javascript
(function () {
'use strict';
angular.module('pdf', []).directive('ngPdf', ['$window', function ($window) {
return {
restrict: 'E',
templateUrl: function (element, attr) {
return attr.templateUrl ? attr.templateUrl : 'app/_infrastructure/pdfViewer/viewer.html';
},
scope: {
pdfUrl: '='
},
link: function (scope, element, attrs) {
var url = scope.pdfUrl;
scope.goPrevious = function () {
if (scope.pageToDisplay <= 1) {
return;
}
scope.pageToDisplay = parseInt(scope.pageToDisplay) - 1;
scope.pageNum = scope.pageToDisplay;
};
}
};
}]);
})();
2-This is the directive template (.hmtl)
<nav class="text-center">
<div class="prev-next-button previous">
<button class="btn btn-danger btn-sm" ng-click="goPrevious()">
<i class="fa fa-arrow-left fa-lg"></i>
</button>
</div>
<div class="prev-next-button next">
<button class="btn btn-danger btn-sm" ng-click="goNext()">
<i class="fa fa-arrow-right fa-lg"></i>
</button>
</div>
<span>Pág: </span>
<input type="text" class="searchPageNumber" min="1" ng-model="pageNum">/
<span>{{pageCount}}</span>
<button class="btn btn-danger btn-sm" title="Diminuir" ng-click="zoomOut()">
<i class="fa fa-minus"></i>
</button>
<button class="btn btn-danger btn-sm" ng-click="fit()">
100%
</button>
<button class="btn btn-danger btn-sm" title="Aumentar" ng-click="zoomIn()">
<i class="fa fa-plus"></i>
</button>
</nav>
<hr style="border-top: 0px; margin-bottom: 0px!important;margin-top: 1px!important" />
<div style="max-height:900px;max-width:1051px;overflow: auto;">
<canvas id="pdf" style="border:2px solid #000000;"></canvas>
</div>
3- This the user page (.html)
<div class="main-content-inner" ng-controller="MyController as Ctrl">
<div class="col-xs-8" >
<div class="form-group" ng-show="(Ctrl.currentProcesso.estadoProcessoId==1 || Ctrl.currentProcesso.estadoProcessoId== 3 ||Ctrl.currentProcesso.estadoProcessoId==6) && Ctrl.appUserBasicInfo.role == 'Escrivão'">
<ng-pdf data-pdf-url="file.pdf" canvasid="pdf" scale="page-fit" page="1"></ng-pdf>
</div>
</div>
<div class="col-xs-3">
<button class="btn btn-success btn-sm btn-next"
ng-click="Ctrl.controllerMethodToCallDirectiveFunction()"
ng-show="true">
NEXT
<i class="tcicons-icon fa fa-arrow-right icon-on-right"></i>
</button>
</div>
</div>
4-Finally this is my controller (MyController.ts)
public enumerarProcessoPushPop = (): boolean => {
//I want to call the directive method here
return true;
}
Methods inside Angular directives are not meant to be called. From the Angularjs documentation:
At a high level, directives are markers on a DOM element (such as an attribute, element
name, comment or CSS class) that tell AngularJS's HTML compiler ($compile) to attach a specified behavior to that DOM element (e.g. via event listeners), or even to transform the DOM element and its children.

Change Text in a button using ng-click

I am trying to change the text in the button using angular I am pretty much done but I have a few questions.
First, my code below just changes the text when I click the first time, how can I change it again after the second click? That button is hiding a <DIV> so that is the reason for the change?
Angular:
angular.module('test',[])
.controller('MyCtrl',function ($scope) {
$scope.myText = 'Press to start';
$scope.start = function () {
$scope.myText = 'Starting...';
}
});
HTML:
<body ng-controller="MyCtrl">
<button ng-click="start()"> {{ myText }} </button>
</body>
The second part of my question is:
How can I apply that code in that code up in that HTML and Angular?
HTML:
<a ng-click="Menu()" class="btn btn-default" id="menu-client">hide the search</a>
Angular:
$scope.Menu = function(){
$("#wrapper").toggleClass("toggled");
};
Changing text:
JS
angular.module('test',[])
.controller('MyCtrl',function ($scope) {
$scope.myText = 'Press to start';
$scope.start = function (myText) {
$scope.myText = (myText === 'Starting...') ? 'Finish' : 'Starting...';
}
});
HTML
<body ng-controller="MyCtrl">
<button ng-click="start(myText)"> {{ myText }} </button>
</body>
Changing class:
JS
$scope.isToggled = false;
HTML
<a ng-click="isToggled = true" class="btn btn-default" ng-class="{toggled: isToggled}" id="menu-client">hide the search</a>
HTML
<a ng-click="Menu()" class="btn btn-default" id="menu-client">hide the search</a>
Angular
$scope.Menu = function(){
$scope.isHedden = true;
};
Now whichever element in the HTML that you want to hide/show, use ngHide directive for that:
<some-element ng-hide="isHidden">Some plain text or value goes here</some-element>
Look out https://docs.angularjs.org/api/ng/directive/ngHide for more details on this directive.

How to properly use ng-init with ng-class

I have a row of buttons and want to initialize the first button as active (as the data associated with it is loaded in my controller's init function). The below HTML works great, but when I click the other two buttons the 'active' class remains on the first button. I want this button set as active on page load and then treated 'normally' (ie: if a different button is clicked remove active class from first button):
<div class="btn-group btn-group-sm">
<button class="btn btn-default" ng-class="{active : isActive}" ng-init="isActive = true" type="button" ng-click="playerMap.clusterToggle(true)">Clustered</button>
<button class="btn btn-default" type="button" ng-click="playerMap.clusterToggle(false)">Unclustered</button>
<button class="btn btn-default" type="button" id="heatmap" ng-click="playerMap.heatmap()">Heatmap</button>
</div>
Should be simple as:
If you are using a loop then its even clean - just pass the index.
<button class="btn btn-default" ng-class="isActive[0] ? 'active' : ''" ng-init="isActive[0]=true" type="button" ng-click="toggleButton(isActive,0)">Clustered</button>
<button class="btn btn-default" ng-class="isActive[1] ? 'active' : ''" type="button" ng-click="toggleButton(isActive,1)">Unclustered</button>
<button class="btn btn-default" ng-class="isActive[2] ? 'active' : ''" type="button" ng-click="toggleButton(isActive,2)">Heatmap</button>
Though this does not answer your initial question, here is a working solution (not very efficient I have to say):
HTML:
<div class='container' ng-controller="GoingStack">
<div switch class="button" ng-class="{'active-button': state}">1</div>
<div switch class="button">2</div>
<div switch class="button">3</div>
</div>
Script:
app.controller('GoingStack', ['$scope', function($scope) {
$scope.state = true;
}]);
var active = document.getElementsByClassName('button');
app.directive("switch", [function() {
return {
link: function(scope, element, attr) {
element.bind('click', function(e){
scope.state = false;
active[0].classList.remove('active-button');
element.addClass('active-button');
});
}
}
}]);

Bootstrap Popover with textarea resetting text value

I have an issue where hiding a bootstrap popover just resets the text content of my textarea within the popover. There are many of these in my page and they are created dynamically in a loop (with int i being the counter). Here is my html:
<button class="btn btn-sm" data-toggle="popover" data-container="body" data-title="FOR EVALUATOR'S ATTENTION" type="button" data-html="true" #*id="commentPopOver-#i"*# #*onclick="getAttention(#i)"*#>
<span class="glyphicon glyphicon-pencil"></span>
</button>
<div class="popoverContent" style="display: none !important">
<div class="form-group">
<input name="values[#i].AttentionComment" id="comment-#i" hidden />
<textarea class="form-control" onchange="updateText(#i)" id="commentText-#i" rows="3">someText</textarea>
</div>
</div>
and my JS:
$(function () {
$("[data-toggle=popover]").popover({
html: true,
content: function () {
return $('.popoverContent').html();
}
});
})
Now I understand that it's just recreating the popover with it's default text on load, but it should at least be keeping the changes and the value of the textarea after it is closed/hidden. I wrote this JS to try and make it populate a separate hidden input to contain the value even after reset but it didn't work:
function updateText(id) {
var newtext = $('#commentText-' + id).val();
$('#comment-' + id).val(newtext);
}
Any ideas?
When you use content: function () {return $('.popoverContent').html();} the set the content of your tooltips, the tooltips content a copy of the HTML code return by $('.popoverContent').html(); The textarea is also a copy and not reference to the original textarea in your DOM.
When a tooltips opens the plugin inserts its HTML (including the copy mentioned above) in the DOM with a random unique ID. The plugin also insert a aria-describedby attribute to the elements that trigger the tooltip (the button in your case). The aria-describedby holds the same unique ID set for the tooltip.
Now you can use the 'hide.bs.popover` event. When the tooltips close you should copy the content of the textarea inside your tooltip to the (hidden) textarea in your DOM
Example
HTML:
<button type="button" id="po1" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="right" data-html="true">
Popover on right
</button>
<div class="popoverContent" style="display: none !important">
<div class="form-group">
<textarea class="form-control" rows="3">someText 1</textarea>
</div>
</div>
<br>
<button type="button" id="po2" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="right" data-html="true">
Popover on right
</button>
<div class="popoverContent" style="display: none !important">
<div class="form-group">
<textarea class="form-control" rows="3">someText 2</textarea>
</div>
</div>
javascript:
$("[data-toggle=popover]").each(function( index ) {
var that = $(this);
$(this).popover({
html: true,
content: function () {
return $('#' + $(this).attr('id') + ' + .popoverContent').html();
}
});
});
$('[data-toggle=popover]').on('hide.bs.popover', function () {
$('#' + $(this).attr('id') + ' + .popoverContent textarea').html( $('#' + $(this).attr('aria-describedby') + ' .popover-content textarea').val());
});
Demo: http://www.bootply.com/DvOYV12bHg

Categories

Resources