Recursive template using ng-include inside a directive template - angularjs

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>

Related

ng-show ng-hide angular questions-answer

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>

angular-ui-tree collapse/expand not working

I try to use angular-ui-tree, but collapse not working. I use example from their github with minor changes, but id did not help.
My example:
http://plnkr.co/edit/MIMQ0GWnhQnZ1AhhKzo5?p=preview
<body ng-controller="tCtrl">
<div class="tree">
<div class="row">
<div class="col-sm-12">
<h3>Basic Example</h3>
<button ng-click="expandAll()">Expand all</button>
<button ng-click="collapseAll()">Collapse all</button>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div ui-tree="" id="tree-root">
<ol ui-tree-nodes="" ng-model="listss">
<li ng-repeat="t in listss" ui-tree-node="">
<div ui-tree-handle="" class="tree-node tree-node-content">
<button data-nodrag="" ng-click="toggle(this)">saad</button>
{{t.name}}
</div>
<ol ui-tree-nodes="" ng-model="t.list" ng-class="{hidden: collapsed}">
<li ng-repeat="s in t.list" ui-tree-node="">
<div ui-tree-handle="" class="tree-node tree-node-content">
<button ng-click="toggle(this)">sad</button>
{{s.name}}
</div>
</li>
</ol>
</li>
</ol>
</div>
</div>
</div>
</div>
</body>
You miss CSS class. Please, insert your file CSS.
.hidden {
display: none!important;
visibility: hidden!important;
}
Github use bootstrap.min.css
Try the following things:
add some options from in your controller:
$scope.treeOptions = {
accept: function (sourceNodeScope, destNodesScope, destIndex) {
return false;
},
};
2.get the treeview:
var getRootNodesScope = function () {
return angular.element(document.getElementById("tree-root")).scope();
};
add the collapse / expand functions to your controller:
$scope.collapseAll = function () {
var scope = getRootNodesScope();
scope.collapseAll();
};
$scope.expandAll = function () {
var scope = getRootNodesScope();
scope.expandAll();
};
Replace the following line :<div ui-tree="" id="tree-root">
with div ui-tree="treeOptions>"
5.Optional: Place the id="tree-root" on your <ol> element.

How to put $last ngRepeat result out of href?

Is there any option to put $last ng-repeat result like this <li class="active">Four</li> instead of <li>Four</li> ?
I have been trying with ng-if but unfortunately it did not give good solution yet :(
DEMO: http://codepen.io/anon/pen/bdvVRe
var app = angular.module('myapp', []);
app.controller('testController', function($scope) {
$scope.mymenu = ["one", "two", "three", "four"];
});
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<div ng-controller="testController" class="container">
<strong>Expecting</strong>
<br />
<ol class="breadcrumb">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li class="active">Four</li>
</ol>
<hr />
<strong>Current result</strong>
<ol class="breadcrumb">
<li ng-repeat="menu in mymenu">{{menu}}</li>
</ol>
</div>
</div>
<li ng-repeat="menu in mymenu" ng-class="{active: $last}">
<a ng-if="!$last" href="#">{{menu}}</a>
<span ng-if="$last">{{menu}}</span>
</li>
Demo: http://codepen.io/anon/pen/domYZL

The `content div of a tab` is not showing up.. I am using AngularJS to accomplish this

In reference to the answer by Sergey Romanov
Only vertical tabs show up... The tab content div never gets displayed when I used his code.
Here is my code
<div class="row">
<div class="col-sm-3">
<ul class="nav nav-tabs nav-stacked nav-pills" role="tablist">
<li ng-class="{'active': view_tab == 'tab1'}">
<a class="btn-lg" ng-click="changeTab('tab1')" href="">My Tab 1</a>
</li>
<li ng-class="{'active': view_tab == 'tab2'}">
<a class="btn-lg" ng-click="changeTab('tab2')" href="">My Tab 2</a>
</li>
</ul>
</div>
<div class="col-sm-9">
<div class="tab-content">
<div class="tab-pane" ng-show="view_tab == 'tab1'">
This is tab 1 content
</div>
<div class="tab-pane" ng-show="view_tab == 'tab2'">
This is tab 2 content
</div>
</div>
var NavController = function ($scope,$http) {
$scope.changeTab = function(tab) {
$scope.view_tab = tab;
}
};
NavController.$inject = ['$scope','$http'];
angular.module('publisherApp').controller('NavController', NavController);
For Bootstrap tab content you need to set which tab is active:
var NavController = function($scope, $http) {
$scope.changeTab = function(tab) {
$scope.view_tab = tab;
}
};
NavController.$inject = ['$scope', '$http'];
angular.module('app', []).controller('NavController', NavController);
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.1/ui-bootstrap-tpls.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app='app' ng-controller='NavController'>
<div class="col-sm-3">
<ul class="nav nav-tabs nav-stacked nav-pills" role="tablist">
<li ng-class="{'active': view_tab == 'tab1'}">
<a class="btn-lg" ng-click="changeTab('tab1')" href="">My Tab 1</a>
</li>
<li ng-class="{'active': view_tab == 'tab2'}">
<a class="btn-lg" ng-click="changeTab('tab2')" href="">My Tab 2</a>
</li>
</ul>
</div>
<div class="col-sm-9">
<div class="tab-content">
<div class="tab-pane" ng-class="{active: view_tab == 'tab1'}">
This is tab 1 content
</div>
<div class="tab-pane" ng-class="{active: view_tab == 'tab2'}">
This is tab 2 content
</div>
</div>
</div>
</div>

Hi, I want to know how I can use ng-click with two parameters within (AngularJS)

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<span class="navbar-brand">Suivi Des Tables</span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="{{item.class}}" ng-repeat="item in menu" ng-class="{active : item == itemcourant}"><a ng-click="selectItem(item);tab={{item.tab}}" href="#{{item.nom}}">{{item.nom}}</a></li>
<li><a ng-click="tab=1" href="#">test</a></li>
</div>
</div>
</div>
Problem is : ng-click="selectItem(item);tab={{item.tab}}"
Please see demo below
You should pass item to controller and make all logic inside controller
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
$scope.menu = [{
tab: "One",
nom: "Link A"
}, {
tab: "Two",
nom: "Link B"
}];
$scope.selectItem = function(item) {
$scope.tab = item.tab;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<div ng-controller="firstCtrl">
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<h2>Tab: {{tab}}</h2>
<div class="navbar-header">
<span class="navbar-brand">Suivi Des Tables</span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="{{item.class}}" ng-repeat="item in menu" ng-class="{active : item == itemcourant}"><a ng-click="selectItem(item)" href="#{{item.nom}}">{{item.nom}}</a>
</li>
<li><a ng-click="tab=1" href="#">test</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
</body>

Resources