ng-show ng-hide angular questions-answer - angularjs

I have a website where i show different questions and when you click in one appear the answer and if you click again dissapear. And i trying to do it in
Angular but for the moment i can't achive it.
<ul id="question-list">
<li>
<h1><a ng-click="answer = !answer" >What is this?</a></h1>
<h3 ng-show="answer"> this</h3>
</li>
<li>
<h1 >What is the question?</h1>
<h3>It </h3>
</li>
<li>
<h1>What is the question?</h1>
<h3> Ipsum.</h3>
</li>
Angular Code
ehlApp.controller('faqController', function ($scope) {
$scope.answer = true;
});

The code you posted worked perfectly in a properly set up app, so I'm guessing that's the problem.
<div ng-app="app" ng-controller="ctrl">
<ul id="question-list">
<li>
<h1><a ng-click="answer = !answer">What is this?</a></h1>
<h3 ng-show="answer"> this</h3>
</li>
<li>
<h1>What is the question?</h1>
<h3>It </h3>
</li>
<li>
<h1>What is the question?</h1>
<h3> Ipsum.</h3>
</li>
</ul>
</div>
<script>
var app = angular.module('app',[]);
app.controller('ctrl', function ($scope) {
$scope.answer = true;
});
</script>

Each question have there own answers so to display the answer attached jsbin
HTML :
<div ng-app="myApp">
<div ng-controller="MyCtrl as vm">
<ul id="question-list" >
<li ng-repeat = "questionAns in vm.questionList">
<h1 ng-click="questionAns.showAnswer = !questionAns.showAnswer"> <a ng-bind="questionAns.question"></a></h1>
<h3 ng-show="questionAns.showAnswer" ng-bind="questionAns.answer"></h3>
</li>
</ul>
</div>
JAVASCRIPT :
var app = angular.module("myApp",[]);
app.controller("MyCtrl", ["$scope", function ($scope) {
var vm = this;
vm.questionList = [{
"question" : "What is this question 1?",
"answer" : "this is answer1"
},{
"question" : "What is this question 2?",
"answer" : "this is answer2"
}];
}]);
jsbin to show hide

You can do that all in the template, example:
<p ng-click="x=!x">Something<span ng-show="x"> else</span>.</p>
Clicking on the <p> will make the <span> show and disappear.

<div ng-app="app" ng-controller="control">
<ul id="question-list">
<li>
<h1><a ng-click="answer = !answer">What is this?</a></h1>
<h3 ng-show="answer"> this</h3>
</li>
,<li>
<h1 ng-click="answer = !answer">What is the question?</h1>
<h3 ng-show="answer">It </h3>
</li>
<li>
<h1 ng-click="answer = !answer">What is the question?</h1>
<h3 ng-show="answer"> Ipsum.</h3>
</li>
</ul>
</div>
<script>
var app = angular.module('app',[]);
app.controller('control', function ($scope) {
$scope.answer = true;
});
</script>

Related

Why ng-repeat not displaying items in div

Here I'm trying to display some items in div During the tracing time i'm getting the values but why its not display in div
app.controller('Helloctrls', ['$scope', '$http', 'Servicemsgs', function ($scope, $http, Servicemsgs) {
$scope.localSearch = function (str) {
var ser = Servicemsgs.GetFun(str.description.CourseId);
ser.then(function (d) {
$scope.SerchDetails = d.data;
})
}
}])
Html
<div ng-controller="Helloctrls">
<div postit"" ng-cloak="" angucomplete-alt id="ex1_dropdown" placeholder="Search countries" pause="100" selected-object="localSearch"
local-data="Countries" search-fields="CourseName" title-field="CourseName" minlength="1" maximumlength="3" input-class="form-control form-control-small" match-class="highlight">
<div ng-repeat="ss in SerchDetails">
<ul>
<li>
{{ss.Action}}
</li>
<li>
{{ss.Countrolle}}
</li>
</ul>
</div>
</div>
</div>
Your response is not an array, it has only 1 object, change like this,
<div ng-repeat="ss in SerchDetails[0]">
<ul>
<li>
{{ss.Action}}
</li>
<li>
{{ss.Countrolle}}
</li>
</ul>
</div>

Recursive template using ng-include inside a directive template

Directive template :
<script type="text/ng-template" id="child-map.html">
<b ng-click="selectNode(child.id)">{{child.title}}</b>
<ul ng-if="child.children.length">
<li ng-repeat="child in child.children" ng-include="clild-map.html"></li>
</ul>
</script>
<b ng-click="selectNode(mapData.id)">{{mapData.title}}</b>
<ul ng-if="mapData.children.length">
<li ng-repeat="child in mapData.children" ng-include="child-map.html" ></li>
</ul>
Directive definition :
function widgetNodeMap () {
var nodeMap = {};
nodeMap.restrict = 'E'
nodeMap.scope = {
'mapData' : '=mapData'
}
nodeMap.controller = ['$scope',function($scope){
// $scope.currentNode = $scope.mapData.id;
$scope.selectNode = function(node_id){
$scope.currentNode = node_id;
}
$scope.getCurrentNode = function(){
return $scope.currentNode;
}
}];
nodeMap.templateUrl = '/static/app/components/widget/widget.view.node-map.html';
return nodeMap
}
Main template :
<widget-node-map map-data="navCtrl.mapData">
</widget-node-map>
But still i cannot get the data listed properly. Can you help me, where Im wrong?
I figured the solution
<script type="text/ng-template" id="child-map.html">
<ul ng-if="child.children.length">
<li ng-repeat="child in child.children">
<a ng-click="selectNode(child)">{{child.title}}</a>
<div ng-include src="'child-map.html'"></div>
</li>
</ul>
</script>
<div class="row">
<div class="col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2 col-sm-12 col-xs-12">
<a ng-click="selectNode(mapData)">{{mapData.title}}</a>
<ul ng-if="mapData.children.length">
<li ng-repeat="child in mapData.children">
<a ng-click="selectNode(child)">{{child.title}}</a>
<div ng-include src="'child-map.html'"></div>
</li>
</ul>
</div>
</div>

AngularJs ng-class not updated when value changes

I've a simple menu where I would to apply the 'active' class to a clicked link.
Here's part of my menu:
<ul>
<li>
<a href="#/c/{{cId}}" ng-class='{"active":tog==1}' ng-click='tog=1'>{{c}}
</a>
</li>
<li>
<a href="#/a/{{a.aId}}" class="tree-toggle"
ng-class='{"active":tog==2}' ng-click='tog=2'>{{a.Name}}
</a>
</li>
</ul>
It's really simple structure as I said before, but when I click on first li "c" the class 'active' is set. Then when I click on the second li element the class 'active' is set also on that element and not removed from the first one and set only on the second.
Any suggestion?
Works for me as:
.active{
font-size: 14pt;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ul ng-app>
<li><a href="#" ng-class='{"active":tog==1}' ng-click='tog=1'>Click-Me {{tog}}</a></li>
<li><a href="#" ng-class='{"active":tog==2}' ng-click='tog=2'>Click-Me {{tog}}</a></li>
</ul>
Here's a solution:
Hope it works for you....
(function() {
'use strict';
angular.module('App', [])
.controller('InputCtrl', ['$scope',
function($scope) {
$scope.cId = 1;
$scope.c = 'some text';
$scope.a = {};
$scope.a.Name = 'some text in a';
}
]);
}());
.active{
background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<div ng-app="App" ng-controller="InputCtrl">
<ul>
<li>
<a ng-href="" ng-class='{"active":tog==1}' ng-click='tog=1'>{{c}}
</a>
</li>
<li>
<a href="" class="tree-toggle" ng-class='{"active":tog==2}' ng-click='tog=2'>{{a.Name}}
</a>
</li>
</ul>
</div>

Need to call ng-switch by url or $location

I am trying to use a url to call a tab element. For example, a url like: localhost:9000/widget&view=map will land on the page with the map tab selected and shown the map tab body.
<div class="search-result-tab" ng-init="selectTab='list'">
<ul class="nav tab-header">
<li ng-class="{active: selectTab=='list'}" ng-click="selectTab='list'; changedTab()">
<a href={{listTabURL}}>List</a>
</li>
<li ng-class="{active: selectTab=='map'}" ng-click="selectTab='map'; changedTab()">
<a href={{mapTabURL}}>Map</a>
</li>
</ul>
<div class="tab-body" ng-switch on="selectTab">
<div class="tab-content" ng-switch-when="list">
<div ng-include="'list.html'"></div>
</div>
<div class="tab-content" ng-switch-when="map">
<div ng-include="'map.html'"></div>
</div>
</div>
</div>
Other urls are:
list: localhost:9000/widget&view=list
map: localhost:9000/widget&view=map
Similar question here. One suggestion is to inject the $location service and switch on $location.path, might be worth a try if you are not going to try ui-router (which you really should look into!)
function Ctrl($scope, $location) {
$scope.pagename = function() { return $location.path(); };
};
<div id="header">
<div ng-switch on="pagename()">
<div ng-switch-when="/home">Welcome!</div>
<div ng-switch-when="/product-list">Our products</div>
<div ng-switch-when="/contact">Contact us</div>
</div>
</div>

Unable to get ng-switch working

I'm new to angular and i'm trying to use ng-switch to dynamically load templates using include in bootstrap modal but click doesn't seem to work. What am i doing wrong here ?
HTML
ModalContent.html
<div ng-controller="Nav" class="modal-body">
<div class="left-nav" ng-include="'LeftNav.html'"></div>
<div class="right-nav" ng-include="'RightNav.html'"></div>
</div>
LeftNav.html
<ul>
<li ng-repeat="item in items">
{{ item }}
</li>
</ul>
RightNav.html
<div ng-switch on="page">
<div ng-switch-when="Item1">
<div ng-include="'Item1.html'"></div>
</div>
<div ng-switch-when="item2">
<div ng-include="'Item2.html'"></div>
</div>
<div ng-switch-when="item3">
<div ng-include="'Item3.html'"></div>
</div>
<div ng-switch-default>
<h1>Default</h1>
</div>
</div>
JS
var app = angular.module('plunker', ['ngRoute', 'ui.bootstrap']);
app.controller('Nav', function($scope) {
$scope.items = ['Item1', 'Item2', 'Item3'];
})
Plnkr - http://plnkr.co/edit/D1tMRpxVzn51g18Adnp8?p=preview
There were a couple of problems.
Here is a working demo: http://plnkr.co/edit/CZPvD373HbCC2Ism0xlA?p=preview
Comments inline:
Controller
app.controller('Nav', function($scope) {
$scope.items = ['Item1', 'Item2', 'Item3'];
$scope.page = $scope.items[0];
$scope.selectItem = function (item) {
$scope.page = item;
}
})
LeftNav template
<ul>
<li ng-repeat="item in items">
<!-- calling `selectItem` on the controller to set the `page`
otherwise `page` will be set on the current `ng-include` scope
and will be unavailable elsewhere -->
{{ item }}
</li>
</ul>
RightNav template
<!-- 'page' is now from the 'Nav' controller's $scope -->
<div ng-switch on="page">
<div ng-switch-when="Item1">
<div ng-include="'Item1.html'"></div>
</div>
<!-- String matches are case sensitive -->
<div ng-switch-when="Item2">
<div ng-include="'Item2.html'"></div>
</div>
<!-- String matches are case sensitive -->
<div ng-switch-when="Item3">
<div ng-include="'Item3.html'"></div>
</div>
<div ng-switch-default>
<h1>Default</h1>
</div>
</div>

Resources