in ng-show not working while clicking reply link - javascript

<html lang="en" ng-app=“myDiscuss">
<head><!-- all the script files-->
</head>
<body>
<div class="thumbnail" ng-controller="TabController as tabs”>
<!-- some other div-->
<div ng-show="tabs.isSelected(1)">
<small>No of likes</small>
</div>
<section class="caption" >
<ul class="nav nav-pills">
<li ng-class="{ active:tab === 1 }" >
<a href ng-click="tabs.selectTab(1)">Helpful</a>
</li>
<li ng-class= "{ active:tab === 2 }">
<a href ng-click="tabs.selectTab(2)">Comment</a>
</li>
</ul>
<div class="panel" ng-show="tabs.isSelected(2)">
<blockquote>
<ul class="nav nav-pills">
<li ng-class="{ active:tab === 3 }" >
<a href ng-click="tabs.selectTab(3)" >Helpful</a>
</li>
<li ng-class= "{ active:tab === 4}">
<a href ng-click="tabs.selectTab(4)">Reply</a>
</li>
</ul>
<input type="text" ng-show="tabs.isSelected(4)">
</blockquote>
</div>
</section>
</div>
</body>
</html>
JavaScript File
var app = angular.module("myDiscuss", []);
app.controller("TabController", function() {
this.tab=0;
this.selectTab = function(setTab) {
this.tab= setTab;
};
this.isSelected = (function(checkTab){
return this.tab === checkTab;
});
});
When I click the "Comment" Link it open the div "panel". whereas when i click "Reply" link it doesn't open input tag.
Image with comment and helpful link
The div "panel" which open when comment link is clicked

Actually, it's working but since you are using same this.tab variable it's not working. You should take separate variable.
Take separate variable for main tabs & sub tabs or you can use another method to separate main tabs & sub tabs. Like 1.1 or 3.3 etc. There are many ways.
this.tab=0;
this.subTab = 0;
this.selectTab = function(setTab) {
this.tab= setTab;
};
this.selectSubTab = function(setTab){
this.subTab = setTab;
};
this.isSelected = (function(checkTab){
return this.tab === checkTab;
});
this.isSelectedSub = (function(checkTab){
return this.subTab === checkTab;
});
check the https://plnkr.co/edit/leTBpJUVC7giDCs9KtQX

For less confusion, I'd recommend assigning the controller to a variable:
var app = angular.module("myDiscuss", []);
app.controller("TabController", function() {
var v = this;
v.tab=0;
v.selectTab = function(setTab) {
v.tab= setTab;
};
v.isSelected = (function(checkTab){
return v.tab === checkTab;
});
});
This is because in function, this refers to the function, not the controller. By using a separate variable, you avoid the inconsistency.
Even the alias you're using, TabController as tabs, uses the same thing:
$scope.tabs = this internally.

Related

Get an element when using ng-if is undefined. Angular.js

All the default elements are hidden.
$scope.hide=false;
ng-if="hide"
I want it when I click on a link, it shows the corresponding element. But I do not know how to show only the element that corresponds. For example if I click on
<li> section1 </ li>
The element with the 'section1' id must be displayed. After that, I do not know why the element is not found.
Var myEl = angular.element (document.querySelector ('#' + id));
Do this using the ng-if statement.
http://jsfiddle.net/8jmnpn2u/
Thank you very much.
Here is your solution
http://jsfiddle.net/8jmnpn2u/2/
Simply you can do it by $scope variable
Define $scope.currentDiv = "";
and on show function just update $scope.currentDiv
Your HTML code
<body ng-app="myApp" ng-controller="myCtrl">
<nav>
<ul>
<li>section1</li>
<li>section2</li>
<li>section3</li>
</ul>
</nav>
<section id='section1' ng-if="currentDiv == 'section1'">
1
</section>
<section id='section2' ng-if="currentDiv == 'section2'">
2
</section>
<section id='section3' ng-if="currentDiv == 'section3'">
3
</section>
</body>
Angular controller
// the main (app) module
var myApp = angular.module("myApp", ['ui.swiper']);
// add a controller
myApp.controller("myCtrl", function($scope) {
$scope.msg = "hello world";
$scope.currentDiv = "";
$scope.show= function(id){
$scope.currentDiv = id;
}
});
Try this: if you create an array to keep the 'hide' value of yor sections, you can control them independently
<body ng-app="myApp" ng-controller="myCtrl">
<nav>
<ul>
<li>section1</li>
<li>section2</li>
<li>section3</li>
</ul>
</nav>
<section id='section1' ng-if="hide['section1']">
1
</section>
<section id='section2' ng-if="hide['section2']">
2
</section>
<section id='section3' ng-if="hide['section3']">
3
</section>
</body>
the main (app) module
var myApp = angular.module("myApp", ['ui.swiper']);
myApp.controller("myCtrl", function($scope) {
$scope.hide = [];
$scope.show= function(id){
$scope.hide[id] = true;
var myEl = angular.element( document.querySelector( '#'+id ));
}
});
Here's the jsfiddle http://jsfiddle.net/2c71bjuu/

how to select by classname in angular from json

I am trying to get an inner value of an element by selecting through the class in angular. But for some reason it keeps giving me an error when I try to select var highlighted = angular.element(element.getElementsByClassName("btn-danger")); This is the error
angular.js:13642TypeError: angular.element.getElementsByClassName is not a function
Could it be because I am mixing angular and jquery together
the main problem is in the click controller.
it gets the value of the ul that gets loaded with the json and supposed to show on the bottom in the mouse click me(the number is just for testing)
the project is a single click keyboard that will eventually have an interval and switch keys so a disabled person can type with only one click
here is the view
<body ng-controller="mainController" ng-click="textArea = textArea + 1">
<div ng-controller="clickController">
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Click and Type</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><i class="fa fa-home"></i> Home</li>
</ul>
</div>
</nav>
</header>
<div class="container">
<div ng-controller="intervalController">
<ul class="general_button" ng-repeat="letter in language[0].rows track by $index" ng-init="rowIndex = $index"> {{rowIndex}}
<button type="button" class="btn " ng-class="{true:'btn-danger', false:'btn-info'}[rowIndex ==isSelected() || columnIndex == isSelected()]" ng-repeat="single in letter track by $index" ng-init="columnIndex = $index">
{{columnIndex}}{{single}}
</button>
</ul>
<div >
<h1 ng-mousemove="textArea = textArea + 1">Mouse over me!</h1>
<label for="inputlg">input-lg</label>
<input class="form-control input-lg" id="inputlg" type="text" value="{{ textArea + highlightedLetter }}">
</div>
</div>
</div>
</div>
</body>
</html>
and here is the model
var App = angular.module('App', []);
var theLanguage = 'english';
App.factory('jsonLanguage', function($http){
var theLanguage = 'english';
return {
get: function(theLanguage){
//var url = theLanguage + '.json';
var url = 'english.json';
return $http.get(url);
}
}
});
App.controller('mainController', function($scope, $http, $log, jsonLanguage) {
jsonLanguage.get().then(function(res){
$scope.language = res.data;
$log.log($scope.language);
});
$scope.isSelected = function(rowIndex, columnIndex) {
return 0;
}
});
App.controller('intervalController', function($scope, $log) {
this.$log = $log;
//var name = $scope.single;
//$log.log(name);
});
App.controller('clickController', function($scope, $log) {
$scope.$log = $log;
var highlighted = angular.element(angular.element.getElementsByClassName("btn-danger"));
alert($scope.highlightedLetter = highlighted.value);
});
I greatly appreciate any help as I am new to angular.
There's no getElementsByClassName method in Jquery. Try using standard DOM query:
var highlighted = angular.element(document.querySelector(".btn-danger"));
getElementsByClassName is a function of DOM element, not jQuery.
Try this, it might help you:
var classElement = document.getElementsByClassName("multi-files");
var highlighted= angular.element(classElement);

AngularJS: Displaying data through a click action in a new page from a speific JSON file

I have a thumb-nail view of two images and I have two JSON files(named dataOne.json and dataTwo.json). When the user click on one of the images, I want to list out all the elements of the respective JSON file into a new web page. The new web page has a pill.
HTML:
<div id = "Cities" class = "neighborhood-guides">
<div class = "container" ng-controller="CityController">
<div class = "thumbnail" ng-click = "city(dataOne)">
<image src = "Images/NYC.jpg"/>
</div>
<div class = "thumbnail" ng-click = "city(dataTwo)">
<image src = "Images/LA.jpg"/>
</div>
</div>
</div>
HTML(new) :
<div class = "content" id = "content">
<div class="list-group-item">
<section class="tab" ng-controller = "TabController as tab">
<ul class="nav nav-pills">
<li ng-class="{ active: tab.isSet(1) }">
<a ng-click = "tab.setTab(1)" href>Categories</a>
</li>
<li ng-class="{ active: tab.isSet(2) }">
<a ng-click = "tab.setTab(2)" href>Interactive Guide</a>
</li>
<li ng-class="{ active: tab.isSet(3) }">
<a ng-click = "tab.setTab(3)" href>List of all places</a>
</li>
</ul>
<div ng-show = "tab.isSet(1)"></div>
<div ng-show = "tab.isSet(2)">
<blockquote>This is where I want the data</blockquote>
</div>
<div ng-show = "tab.isSet(3)">
<blockquote></blockquote>
</div>
</section>
</div>
</div>
app.js
function() {
var app = angular.module('app', []);
app.controller('CityController', function(){
$scope.city = function(param) {
$http.get(param+'.json', param).success(
function(data){
//What should I do here?
console.log(data);
}
).error();
}
});
app.controller('TabController', function(){
this.tab = 1;
this.setTab = function(newValue){
this.tab = newValue;
};
this.isSet = function(tabName){
return this.tab === tabName;
};
);
How do I get the data from the json file, through the click from page 1 and display it in the pill 2 of the new page?
you can define a service factory which holds the json data. please take a look this answer for similar question:
https://stackoverflow.com/a/21920241/4213391

Directive changes CSS when element is active

I'm very new to Angular so I'm sure this isn't correct but I have a set of <li> elements which further contain links (individually, they're just navigation buttons for my page). What I'd like is to keep the specific button, say, "Home" highlighted a certain color while the user is on that page. So my html roughly looks like this...
<li class="active menuBar">
<a ui-sref="aap.configure.views" change-css="aap.isActive()">
<div class="row text-center">
<span class="glyphicon glyphicon glyphicon-cog" aria-hidden="true" style="font-size: 2.5em;"></span>
</div>
<div class="row">
<span class="glyphicon-class">Home</span>
</div>
</a>
</li>
I literally just wrote my first custom Directive yesterday so here's my second attempt at one...
app.directive( 'changeCss', [ '$location',
function ( ) {
return {
restrict: 'A',
scope: {
active: '&'
},
link: function( $scope, element, attrs ){
if( $scope.active ){
element.addClass( 'actives');
}
else {
element.addClass( 'inActives' );
}
}
};
}
] );
And my isActive() function looks like...
var active = false;
$scope.aap.isActive = function( ){
if ( active ){
active = false;
return true;
} else {
active = true;
return false;
};
};
Now, when I load up the page all the navigation buttons (Home, Save, etc.) are the color I'd like them to be. Clicking on a button, say, "Home" highlights that button in the color I'd like it to be. But, when I interact with that Home page at all, click an accordion element for instance, the color goes back to the "inactive" color. What am I doing wrong?
I'm not 100% sure for this. but I hope this might help you. If you want to set the active/inactive of your navigation bar base on your page.
I use ng-class for setting active of the navigation bar (see here https://docs.angularjs.org/api/ng/directive/ngClass) by passing the uri to the function getClass().
<div class="col-sm-3 col-md-2 sidebar" ng-controller="mainCtrl">
<ul class="nav nav-sidebar">
<li ng-class="{'active': getClass('/dashboard')}" >
<a href="/">
Dashboard
</a>
</li>
<li ng-class="{'active': getClass('/session')}">
<a ng-href="/session">
Sessions
</a>
</li>
</ul>
</div>
at my Controller I check the location path and sends boolean for activating/deactivating the ng-class
.controller('mainCtrl', ['$location','$scope', function ($location, $scope) {
$scope.getClass = function(path) {
if ($location.path().substr(0, path.length) == path) {
return true;
} else {
return false;
}
}
}]);

How do I toggle an ng-show in AngularJS based on a boolean?

I have a form for replying to messages that I want to show only when isReplyFormOpen is true, and everytime I click the reply button I want to toggle whether the form is shown or not. How can I do this?
You just need to toggle the value of "isReplyFormOpen" on ng-click event
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-show="isReplyFormOpen" id="replyForm">
</div>
Basically I solved it by NOT-ing the isReplyFormOpen value whenever it is clicked:
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-init="isReplyFormOpen = false" ng-show="isReplyFormOpen" id="replyForm">
<!-- Form -->
</div>
If based on click here it is:
ng-click="orderReverse = orderReverse ? false : true"
It's worth noting that if you have a button in Controller A and the element you want to show in Controller B, you may need to use dot notation to access the scope variable across controllers.
For example, this will not work:
<div ng-controller="ControllerA">
<a ng-click="isReplyFormOpen = !isReplyFormOpen">Reply</a>
<div ng-controller="ControllerB">
<div ng-show="isReplyFormOpen" id="replyForm">
</div>
</div>
</div>
To solve this, create a global variable (ie. in Controller A or your main Controller):
.controller('ControllerA', function ($scope) {
$scope.global = {};
}
Then add a 'global' prefix to your click and show variables:
<div ng-controller="ControllerA">
<a ng-click="global.isReplyFormOpen = !global.isReplyFormOpen">Reply</a>
<div ng-controller="ControllerB">
<div ng-show="global.isReplyFormOpen" id="replyForm">
</div>
</div>
</div>
For more detail, check out the Nested States & Nested Views in the Angular-UI documentation, watch a video, or read understanding scopes.
Here's an example to use ngclick & ng-if directives.
Note: that ng-if removes the element from the DOM, but ng-hide just hides the display of the element.
<!-- <input type="checkbox" ng-model="hideShow" ng-init="hideShow = false"></input> -->
<input type = "button" value = "Add Book"ng-click="hideShow=(hideShow ? false : true)"> </input>
<div ng-app = "mainApp" ng-controller = "bookController" ng-if="hideShow">
Enter book name: <input type = "text" ng-model = "book.name"><br>
Enter book category: <input type = "text" ng-model = "book.category"><br>
Enter book price: <input type = "text" ng-model = "book.price"><br>
Enter book author: <input type = "text" ng-model = "book.author"><br>
You are entering book: {{book.bookDetails()}}
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('bookController', function($scope) {
$scope.book = {
name: "",
category: "",
price:"",
author: "",
bookDetails: function() {
var bookObject;
bookObject = $scope.book;
return "Book name: " + bookObject.name + '\n' + "Book category: " + bookObject.category + " \n" + "Book price: " + bookObject.price + " \n" + "Book Author: " + bookObject.author;
}
};
});
</script>
If you have multiple Menus with Submenus, then you can go with the below solution.
HTML
<ul class="sidebar-menu" id="nav-accordion">
<li class="sub-menu">
<a href="" ng-click="hasSubMenu('dashboard')">
<i class="fa fa-book"></i>
<span>Dashboard</span>
<i class="fa fa-angle-right pull-right"></i>
</a>
<ul class="sub" ng-show="showDash">
<li><a ng-class="{ active: isActive('/dashboard/loan')}" href="#/dashboard/loan">Loan</a></li>
<li><a ng-class="{ active: isActive('/dashboard/recovery')}" href="#/dashboard/recovery">Recovery</a></li>
</ul>
</li>
<li class="sub-menu">
<a href="" ng-click="hasSubMenu('customerCare')">
<i class="fa fa-book"></i>
<span>Customer Care</span>
<i class="fa fa-angle-right pull-right"></i>
</a>
<ul class="sub" ng-show="showCC">
<li><a ng-class="{ active: isActive('/customerCare/eligibility')}" href="#/CC/eligibility">Eligibility</a></li>
<li><a ng-class="{ active: isActive('/customerCare/transaction')}" href="#/CC/transaction">Transaction</a></li>
</ul>
</li>
</ul>
There are two functions i have called first is ng-click = hasSubMenu('dashboard'). This function will be used to toggle the menu and it is explained in the code below. The ng-class="{ active: isActive('/customerCare/transaction')} it will add a class active to the current menu item.
Now i have defined some functions in my app:
First, add a dependency $rootScope which is used to declare variables and functions. To learn more about $roootScope refer to the link : https://docs.angularjs.org/api/ng/service/$rootScope
Here is my app file:
$rootScope.isActive = function (viewLocation) {
return viewLocation === $location.path();
};
The above function is used to add active class to the current menu item.
$rootScope.showDash = false;
$rootScope.showCC = false;
var location = $location.url().split('/');
if(location[1] == 'customerCare'){
$rootScope.showCC = true;
}
else if(location[1]=='dashboard'){
$rootScope.showDash = true;
}
$rootScope.hasSubMenu = function(menuType){
if(menuType=='dashboard'){
$rootScope.showCC = false;
$rootScope.showDash = $rootScope.showDash === false ? true: false;
}
else if(menuType=='customerCare'){
$rootScope.showDash = false;
$rootScope.showCC = $rootScope.showCC === false ? true: false;
}
}
By default $rootScope.showDash and $rootScope.showCC are set to false. It will set the menus to closed when page is initially loaded. If you have more than two submenus add accordingly.
hasSubMenu() function will work for toggling between the menus. I have added a small condition
if(location[1] == 'customerCare'){
$rootScope.showCC = true;
}
else if(location[1]=='dashboard'){
$rootScope.showDash = true;
}
it will remain the submenu open after reloading the page according to selected menu item.
I have defined my pages like:
$routeProvider
.when('/dasboard/loan', {
controller: 'LoanController',
templateUrl: './views/loan/view.html',
controllerAs: 'vm'
})
You can use isActive() function only if you have a single menu without submenu.
You can modify the code according to your requirement.
Hope this will help. Have a great day :)

Categories