Submenu doesn´t show/hide on my ng-click($index) - angularjs

I'm trying to do a tree menu on my site.
I want the subitems to show/hide when I click the parent.
This solution don't work. Even tho it changes the item.active value to false/true. Any ides my friends?
Here is my HTML:
<div id="sidebar-wrapper">
<div class="cssTreeMenu tree">
<ul class="sidebar-nav">
<li ng-repeat="item in items" >
<a ui-sref="{{item.sref}}" ui-sref-active="active" ng-click="testcolaps($index)">{{item.name}}</a>
<ul ng-show="{{item.active}}" class="navbarColorOnClick">
<li ng-repeat="subItem in item.subItems" ui-sref-active="active">
<a ui-sref="{{subItem.sref}}">{{subItem.name}}</a>
</li>
</ul>
</li>
</ul>
</div>
Here is my controller:
$scope.testcolaps = function (index) {
$scope.items[index].active = !$scope.items[index].active;
};
$scope.items = [
{
name: "Test",
sref: "kpi.test",
active: false,
subItems: [
{
name: "Selectable KPI menu",
sref: "kpi.selectableKpiMenu"
},
{
name: "Test",
sref: "kpi.test"
}
]
},
{
name: "Test2",
sref: "kpi.test",
active: false,
subItems: [
{
name: "Selectable KPI menu 2",
sref: "kpi"
},
{
name: "Test 2",
sref: "internal"
}
]
},
];
As you can se my items have a active: property who I change to true/false. But still nothing happens in my menu. If I hard-code it to true it is showing like it should but still the click-event doesn't work.

The expression inside the ng-show need an expression without {{}} interpolation. Basically you need to remove {{}}.
ng-show="item.active"

Related

Is there any way to bind the function name from a JSON data to the (click) of a button in angular

Hi I want to assign method name dynamically and use them.Please help.
I am trying something like following.
<div class="dashboard row">
<div class="card col-lg-3" *ngFor="let card of cards">
<h5 class="card-title">{{card.title}}</h5>
<div class="card-body">
<ul style="list-style-type:none;">
<li *ngFor="let cardFeature of cardFeatures" style="padding-left: 7px;">
{{cardFeature.title}}
<button (click)="[cardFeature.method]">{{cardFeature.method}}</button>
</li>
</ul>
</div>
</div>
CardFeatures is an array where I am defining the JSON data and there I am defining my method name so that I can dynamically read call that. But I am unable to achieve the goal.
Please help to resolve the same.
This is what I have in my ts file
cardFeatures = [
{
"name": "id",
"title": "ID",
"enable": true,
"link": "/register",
"method": "meth1()"
},
{
"name": "profile",
"title": "profile",
"enable": true,
"link": "/link1",
"method": "meth2()"
}
]
cards = [
{
title: "Card1"
},
{
title: "Card2"
}]
Try like this:
Working Demo
cardFeatures = [
{
name: "id",
title: "ID",
enable: true,
link: "/register",
method: () => this.meth1()
},
{
name: "profile",
title: "profile",
enable: true,
link: "/link1",
method: () => this.meth2()
}
];
Template:
<div class="card-body">
<ul style="list-style-type:none;">
<li *ngFor="let cardFeature of cardFeatures" style="padding-left: 7px;">
{{cardFeature.title}}
<button (click)="cardFeature.method()">Test</button>
</li>
</ul>
</div>
EDIT:
As per your comment, you can also try this solution

How to call another key from ng-repeat source

Pardon me if the question sounds weird. Suppose I have the following object:
// For example purposes, pretend that link gets pushed to $scope.links 5 times.
var link = {
name: "name",
description: "description",
members: [{
memname: "member name"
}],
active: true
}
$scope.links.push(link);
...and I used $scope.links as follows
<li ng-repeat="item in links">
<li ng-repeat = "member in item.members">
...
</li>
</li>
...and inside the list with member in item.members, I want to use link.active for ng-show. Is it possible to do so?
Something like:
<li ng-repeat="member in item.members" ng-show="item.active">.
Please advise.
Thank you.
Created Snippet for you .
function myCtrl($scope) {
$scope.links = [{
'name': "name",
'description': "description",
'members': [{
'memname': "member name"
},{
'memname': "member name 2"
}],
'active': true
},{
'name': "name 2",
'description': "description",
'members': [{
'memname': "member name"
},{
'memname': "member name 2"
}],
'active': false
}]
//$scope.links.push(link);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app ng-controller="myCtrl">
<ul>
<li ng-repeat="item in links" ng-show="item.active == true">
{{item.name}}
<ul >
<li ng-repeat = "member in item.members" >
{{member.memname}}
</li>
</ul>
</li>
</ul>
</div>
Hope this will help you.

Trigger Angular scope id remotely outside of ng-repeat

I am looking for some help with a little Angular App.
I have a group of items/images that when a plus icon is clicked, all other items are covered and the selected item is not covered.
I want to now trigger this from outside of the ng-repeat.
I am looking for a way to trigger the cover effect when clicking on a text link, as well as the plus icon now used.
Have it use both options.
So far I have the text triggers fire off a id, that can be used in the app code.
Here is a Working FIDDLE.
Here is the original question that Avijit Gupta did for this.
Avijit's post is the excepted answer.
Any help with this is greatly appreciated.
The nav Text trigger code...
<div class="col-xs-12 navText">
<ul>
<li><a href data-id='1' data-ng-click="idLocation($event)">ONE</a></li>
<li><a href data-id='2' data-ng-click="idLocation($event)">TWO</a></li>
<li><a href data-id='3' data-ng-click="idLocation($event)">THREE</a></li>
<li><a href data-id='4' data-ng-click="idLocation($event)">FOUR</a></li>
<li><a href data-id='5' data-ng-click="idLocation($event)">FIVE</a></li>
</ul>
</div>
The app code...
// nav text trigger id's
$scope.idLocation = function (e) {
ids = $(e.target).data('id');
console.log(ids);
};
$scope.setToInitialState = function() {
$scope.blocks.forEach(function(block) {
$scope.isSelected[block.id] = false;
$scope.isCovered[block.id] = false;
});
};
$scope.selectBlock = function(id) {
$scope.isSelected[id] = !$scope.isSelected[id];
$scope.isCovered[id] = false;
if ($scope.isSelected[id]) {
$scope.blocks.forEach(function(block) {
if (block.id !== id) {
$scope.isCovered[block.id] = true;
$scope.isSelected[block.id] = false;
}
});
}
else {
$scope.setToInitialState();
}
};
});
How it works...
I suggest you to use the same ng-repeat in tag li and the same function selectBlock in tag a.
<div class="row">
<div class="col-xs-12 navText">
<ul>
<li ng-repeat="block in blocks"><a href data-ng-click="selectBlock(block.id)">{{block.name}}</a></li>
</ul>
</div>
</div>
Also I add the field name for each element in blocks.
$scope.blocks = [
{
id: '1',
name: 'ONE',
block: "1",
},
{
id: '2',
name: 'TWO',
block: "2",
},
{
id: '3',
name: 'THREE',
block: "3",
},
{
id: '4',
name: 'FOUR',
block: "4",
},
{
id: '5',
name: 'FIVE',
block: "5"
}
];
To Answer my own question.
One way to trigger the cover effect from outside of the ng-repeat.
What I did here was to use the selectBlock() in a data-ng-click and just hard code the ID into the selectBlock().
Like this...
<li><a href data-ng-click="selectBlock('1')">ONE</a></li>
<li><a href data-ng-click="selectBlock('2')">TWO</a></li>
<li><a href data-ng-click="selectBlock('3')">THREE</a></li>
<li><a href data-ng-click="selectBlock('4')">FOUR</a></li>
<li><a href data-ng-click="selectBlock('5')">FIVE</a></li>
Here is the demo of it working by clicking the TEXT and the +/- icon.
Working FIDDLE.

How to show hierarchical data in tabular format using ng-repeat

i am new in angular. so i was reading write up just to accomplish my job. this way i will try but not sure that am i going in right direction? here is my sample code
<ul>
<li ng-repeat="parent in items">{{parent.pitem}}
<ul>
<li ng-repeat="child in parent.citems">{{child.pitem }}</li>
</ul>
</li>
</ul>
var app = angular.module('myapp', []);
app.controller('MyCtrl', function ($scope) {
$scope.items = [{ "pitem": "Coffee", "citems": "" }, { "pitem": "Tea", "citems": [{ "pitem": "Black tea", "citems": "" }, { "pitem": "Green tea", "citems": "" }] }, { "pitem": "Milk", "citems": "" }]
});
i actually i need to show employee hierarchy like manager and their subordinate.
i also read this too on the same kind of topic written by #zsong https://stackoverflow.com/a/18295177/6188148
so please help me to generate tabular output with ng-repeat using ul,li and end user should be able to expand collapse rows too. please have a look at this url http://maxazan.github.io/jquery-treegrid/examples/example-bootstrap-3.html i want similar kind of output but with ng-repeat.
see this url http://www.lidorsystems.com/support/articles/angularjs/treegrid/tree-grid-filter.aspx then can understand what kind of output i am trying to build with ng-repeat.
please guide and suggestion.
This JSFiddle https://jsfiddle.net/benfosterdev/NP7P5/ shows exactly what you are looking for:
Recursion
<div ng-app="app" ng-controller='AppCtrl'>
<script type="text/ng-template" id="categoryTree">
{{ category.title }}
<ul ng-if="category.categories">
<li ng-repeat="category in category.categories" ng-include="'categoryTree'">
</li>
</ul>
</script>
<ul>
<li ng-repeat="category in categories" ng-include="'categoryTree'"></li>
</ul>
</div>
Json object
$scope.categories = [
{
title: 'Computers',
categories: [
{
title: 'Laptops',
categories: [
{
title: 'Ultrabooks'
},
{
title: 'Macbooks'
}
]
},
{
title: 'Desktops'
},
{
title: 'Tablets',
categories: [
{
title: 'Apple'
},
{
title: 'Android'
}
]
}
]
},
{
title: 'Printers'
}
];
});

AngularJS ngrepeat - filter based on scope data

I am trying to create a dropdown menu using angular ng-repeat.
I have a jd object with a field called parent_id which indicates parent node under which this node should show up. Help I need is to create filter based on previous filtered data as shown in the markup
My markup code:
<div >
<ul class="nav nav-pills" data-ng-controller= "MenuController" >
<li data-ng-class="{'active':getClass('/customers')}"
data-ng-repeat="menuItem in menuItems | filter: { ParentId: '0' }" >
{{ menuItem.Name }}
**<ul>
<li data-ng-repeat="menuItem1 in menuItems | filter: { ParentId: {{ menuItem1.ParentId }} }">
{{ menuItem1.Name }}
</li>
</ul>**
</li>
</ul>
</div>
My Service:
app.service('menuService', function () {
this.getMenuItems = function () {
return menuItems;
};
var menuItems = [
{
id: 'ABCDFER1', Name: 'Apperal', ParentId: 0, description: 'Beautifull Apparels'
},
{
id: 'ABCDFER2', Name: 'Electronics', ParentId: 0, description: 'Electronic bargains'
},
{
id: 'ABCDFER3', Name: 'Home & Kitchen', ParentId: 0, description: 'For your kitchen'
},
{
id: 'ABCDFER4', Name: 'Services', ParentId: 0, description: 'Services for you'
},
{
id: 'ABCDFER5', Name: 'Men', ParentId: 'ABCDFER1', description: 'Men Apperal'
},
{
id: 'ABCDFER6', Name: 'Women', ParentId: 'ABCDFER1', description: 'Women Apperal'
}
];
My controller:
$scope.menuItems = menuService.getMenuItems();
If I correctly understand what you're trying to accomplish, I believe you want this:
<div >
<ul class="nav nav-pills" data-ng-controller= "MenuController" >
<li data-ng-class="{'active':getClass('/customers')}"
data-ng-repeat="menuItem in menuItems | filter: { ParentId: '0' }" >
{{ menuItem.Name }}
<ul>
<li data-ng-repeat="menuItem1 in menuItems | filter: { ParentId: menuItem.id }">
{{ menuItem1.Name }}
</li>
</ul>
</li>
</ul>
</div>
Changes of note:
You want to filter on the id of the parent item, not the parentId of
the current item.
You don't need {{ }} around the filter value, because this is interpreted as code, not a template.

Resources