NgRepeat on switch case - angularjs

I'm using angular include to insert the html's in the page and below is the code for it.
RightNav.html
<div ng-switch on="page">
<div ng-switch-when="Games">
<div ng-include="'Games.html'"></div>
</div>
<div ng-switch-when="Music">
<div ng-include="'Music.html'"></div>
</div>
<div ng-switch-when="Videos">
<div ng-include="'Videos.html'"></div>
</div>
</div>
LeftNav.html
<ul>
<li ng-repeat="item in items">
{{item.name}}
</li>
</ul>
JS
app.controller('Nav', function($scope) {
$scope.items = [
{
name : 'Music'
},
{
name : 'Videos'
},
{
name : 'Games'
}
];
$scope.page = $scope.items[0].name;
$scope.selectedItem = function(item) {
$scope.page = item.name;
}
})
I tried using ng-repeat in RightNav.html and it doesn't work what am i doing wrong here?
<div ng-switch on="page">
<div ng-repeat="item in items">
<div class="item.name" ng-switch-when="{{item.name}}">
<div ng-include="'{{item.name}}.html'"></div>
</div>
</div>
</div>
Demo : http://plnkr.co/edit/D1tMRpxVzn51g18Adnp8?p=preview

In your case you do not need ng-repeat. You can directly bind the expression to ng-include. Also when i tried ng-switch when part does not take expression. Also using the . notation for selected page as the ng-switch and ng-include creates new scope.
See my fiddle
http://plnkr.co/edit/dPILzsyFoWHfL3Urlso2?p=preview

Related

angularjs ng-repeat on different arrays depending on condition

I have two arrays. If ctrl.type == 'group' I want to do an ng-repeat
<div ng-repeat="category in ctrl.categories">
...
<div ng-repeat="item in category.group_items">...</div>
</div>
else, it should be
<div ng-repeat="category in ctrl.categories">
...
<div ng-repeat="item in category.data.other_items">...</div>
</div>
what's the best way to write this? I don't want to rewrite the ... as it's the same code.
Make a function that returns the array in your controller.
ctrl.getArray = () => {
if(ctrl.type === 'something') {
return ctrl.data.group_items;
}
return ctrl.data.other_items;
}
And in the view:
<div ng-repeat="item in ctrl.getArray()">...</div>
It can also be done using ng-switch. Below is my controller code where i have defined two arrays and also defined type on which your data will be shown.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.type = 'something';
$scope.categories=[{ID:'1',Name:'Test 1'},{ID:'2',Name:'Test 2'},{ID:'3',Name:'Test 3'}];
$scope.group_items=[{ID:'1',Name:'Group 1'},{ID:'2',Name:'Group 2'},{ID:'3',Name:'Group 3'}];
});
Below is the html bindings.
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-switch="type">
<div ng-switch-when="group">
<div ng-repeat="item in categories">
<p>{{item.Name}}</p>
</div>
</div>
<div ng-switch-when="something">
<div ng-repeat="item in group_items">
<p>{{item.Name}}</p>
</div>
</div>
</div>
</div>

set dragable false for particular div in angular js with ionic

This is used for match the following option.
While I drag the div id 'div2' along with this first div id 'div1' also to be dragged. how to set draggable false for div id 'div1'
.controller('BEGINNER_UNIT_1_CONVERSATION_ACTIVITY_2', function($scope, $stateParams, $http)
{
$scope.OnDropComplete = function (index, source)
{
var otherObj = $scope.category_Question[index];
var otherIndex = $scope.category_Question.indexOf(source);
$scope.category_Question[index] = source;
$scope.category_Question[otherIndex] = otherObj;
}
})
--------------------------------------------------------
<div ng-repeat="source in category_Question" ng-controller="BEGINNER_UNIT_1_CONVERSATION_ACTIVITY_2" >
<div class="card" id="div1" draggable="false">
<div class="item item-divider">{{ $index+1 }}.Sentence </div>
<div class="item item-text-wrap" style="color:#CC0066;font-weight:bold;">{{ source.Sentance }}</div>
</div>
<div class="card" id="div2" ng-drop="true" ng-drop-success="OnDropComplete($index,$data)">
<div ng-drag="true" ng-drag-data="source" ng-class="source.Response">
<div class="item item-divider">Response</div>
<div class="item item-text-wrap"> {{ source.Response }} </div>
</div>
</div>
</div>
--------------------------------------------------------
You're using the wrong attribute. draggable="false".
It should be ng-cancel-drag
Your html would look like:
<div class="card" id="div1" ng-cancel-drag>
...
</div>

Angular JS - Identifying type of isolated category in child directive

I have a custom directive with isolated scope for category :
'use strict';
angular.module('myModule').directive('myContent', function() {
return {
restrict: 'E',
templateUrl: 'myContent.html',
scope: {
category: '='
},
controller: function($scope){
// private methods
},
controllerAs: 'myContentCtrl'
};
});
In my main directive am calling the same directive as :
<div>
<my-content category = "category1"></my-content>
</div>
<div>
<my-content category = "category2"></my-content>
</div>
<div>
<my-content category = "category3"></my-content>
</div>
And myContent.html is having :
<div class="form-group">
<div class="col-lg-10 navbar">
<ul class="nav nav-tabs" role="tablist">
<li class="active itemFirst"><a href="#link1" role="tab"
data-toggle="tab">Link 1 </a>
</li>
<li>Link 2 </li>
<li>Link 3 </li>
</ul>
</div>
</div>
<div class="form-group">
<div class="tab-content col-lg-10">
<div class="tab-pane active" id="link1">
// contents here for link 1
</div>
<div class="tab-pane" id="link2">
// contents here for link 2
</div>
<div class="tab-pane" id="link3">
// contents here for link 3
</div>
</div>
</div>
Here my issue is, the id is getting replicating as the same direct is populating each links.
So the UI is not populating the different directives and tabs properly.
How can I separate the Id of each one by identifying the category value I had send to each directive.
My requirement is making id dynamic as :
id = "link1_{{category}}"
But it is not reflecting. How can I achieve this?
Updating the category object.
My category objects are having some data like :
$scope.category1 = [
{obj1},
{obj2},
{obj3},
{obj4}
];
$scope.category2 = [
{obj1},
{obj2},
{obj3},
{obj4}
];
So I can't use category1 object as an ID. instead how can i use the string "category1" that I'm separating each categories. I hope now the code is clear.
I just resolved it in a complicated approach, but am not sure about the perfection.
I had added one more data in category object as "type"
Now my category object is:
$scope.category1 = {
type : "category1",
data : [
{obj1},
{obj2},
{obj3},
{obj4}
]
};
$scope.category2 = {
type : "category2",
data : [
{obj1},
{obj2},
{obj3},
{obj4}
]
};
So in HTML the id is :
<div class="form-group">
<div class="col-lg-10 navbar">
<ul class="nav nav-tabs" role="tablist">
<li class="active itemFirst"><a href="#{{category.type}}" role="tab"
data-toggle="tab">Link 1 </a>
</li>
<li>Link 2 </li>
<li>Link 3 </li>
</ul>
</div>
</div>
<div class="form-group">
<div class="tab-content col-lg-10">
<div class="tab-pane active" id="{{category.type}}">
// contents here for link 1
</div>
<div class="tab-pane" id="{{category.type}}">
// contents here for link 2
</div>
<div class="tab-pane" id="{{category.type}}">
// contents here for link 3
</div>
</div>
</div>
</div>
I think you need different ID for each <div> for all instances of directive.
<div class="tab-pane active" id={{'link1_'+category}}></div>
and use # in isolated scope to get values of attributes
app.directive('myContent', function() {
return {
restrict: 'E',
templateUrl: 'myContent.html',
scope: {
category : '#'
},
controller: function($scope){
// private methods
console.log($scope.category)
},
controllerAs: 'myContentCtrl'
};
});
Here's the working plunkr
If I understood you correctly, You can set the Id in a function inside your controller...
<div class="tab-pane active" id="{{setId}}">
And in your custom directive with isolated scope for category :
controller: function($scope){
// private methods
$scope.setId = function(){...}
},

Updating Radio Button Select on Click Parent Element in Angular

Assume this model:
<li ng-repeat="item in documentViews" for ={{item}}>
<div class="col-sm-12 document-view-item" ng-click="change($index)">
<div class="row">
<img class="document-view-img" src="../assets/images/kml_document.png">
</div>
<div class="row">
<input type="radio" ng-model="prefs.documentView" ng-value="item.value" id="{{item.value}}" name="documentView" ng-selected="prefs.documentView">
{{item.key}}
</input>
</div>
</div>
</li>
And this controller:
angular.module('app')
.controller('DocumentViewCtrl', function ($scope) {
$scope.subtitle = 'Default document view';
$scope.documentViews = [
{
key: 'Blah1',
value: 'b1'
},
{
key: 'Blah2',
value: 'b2'
},
];
$scope.change = function($index) {
console.log($scope.documentViews[$index].value);
};
});
I do not want to update the radio button only when selecting the input tag, but when clicking on the higher div element where the ng-click appears. I couldn't get further than triggering a console log when a click occurs and this is fine, but I am wondering how to update the select on the radio button and thus the $scope.
What about something like this?
In your html:
<li ng-repeat="item in documentViews" for="{{item}}">
<div class="col-sm-12 document-view-item" ng-click="change(item)">
<div class="row">
<img class="document-view-img" src="../assets/images/kml_document.png">
</div>
<div class="row">
<input type="radio" ng-model="prefs.documentView" ng-value="item.value" id="{{item.value}}" name="documentView" ng-selected="prefs.documentView">
{{item.key}}
</input>
</div>
</div>
</li>
In your controller:
$scope.change = function(item) {
$scope.prefs.documentView = item.value;
}

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