Clone row of elements in AngularJS - angularjs

I have this Angular form http://plnkr.co/edit/?p=streamer&s=ph0QHW513czywawl.
I need to clone the row on clicking ADD (+) and delete selected row on clicking DELETE (-).
Looking for a solution in AngularJS only. In current solution, the scopes are not working correctly. Also did not yet figure out how to implement (-) functionality.
index.html
<div ng-controller="MyCtrl" style="padding: 10px;">
<br/>
<div style="width: 90%; display: inline-block; border: 1px silver solid;">
<div class="row">
<div class="col-xs-3">
<select class="form-control" data-ng-model="hr.langauge.level" tooltip="Level">
<option value="Native" ng-selected="true">Native</option>
</select>
</div>
<div class="col-xs-4">
<select class="form-control" data-ng-model="hr.langauge.name" tooltip="Language">
<option value="">Language</option>
<option value="EN">English</option>
<option value="IT">Italian</option>
<option value="DE">German</option>
<option value="FR">French</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" data-ng-model="hr.langauge.remark" class="form-control" placeholder="Remark" tooltip="Remark">
</div>
<div class="col-xs-2">
</div>
</div>
<div select-last ng-repeat='item in items'></div>
</div>
<a class="btn" style="margin-bottom: 27px;" href="#" tooltip="Add" ng-click='addRow()'>
<i class="glyphicon glyphicon-plus"></i>
</a>
{{hr.langauge | json}}
language.html
<div class="row" style="padding-top: 5px;">
<div class="col-xs-3">
<select class="form-control" data-ng-model="hr.langauge.level" tooltip="Level">
<option value="">Level</option>
<option value="proficient">Proficient</option>
<option value="intermediate">Intermediate</option>
<option value="beginner">Beginner</option>
</select>
</div>
<div class="col-xs-4">
<select class="form-control" data-ng-model="hr.langauge.name" tooltip="Language">
<option value="">Language</option>
<option value="EN">English</option>
<option value="IT">Italian</option>
<option value="DE">German</option>
<option value="FR">French</option>
</select>
</div>
<div class="col-xs-3">
<input type="text" data-ng-model="hr.langauge.remark" class="form-control" placeholder="Remark" tooltip="Remarks">
</div>
<div class="col-xs-2">
<a class="btn" href="#" tooltip="Delete" ng-click="deleteRow({{$index}});">
<i class="glyphicon glyphicon-minus"></i>
</a>{{$index}}
</div>
</div>
script.js
var myApp = angular.module('myApp',[]);
myApp.directive('selectLast', function () {
return {
restrict: 'A',
templateUrl: 'language.html'
}
});
function MyCtrl($scope) {
$scope.items = [];
$scope.newitem = '';
$scope.addRow = function(){
$scope.items.push($scope.newitem);
console.log('+ clicked');
}
$scope.deleteRow = function(rowNo) {
/*$scope.items.splice($scope.newitem);*/
console.log('- clicked in row ' + rowNo);
}
}

Based on your comment and plunker, it looks like you have the Add figured out. The remove row is easier. All you need to do is splice out the row. The issue on your view is you have the index surrounded by {{}} which isn't necessary.
$scope.deleteRow = function(rowNo) {
/*$scope.items.splice($scope.newitem);*/
$scope.languages.splice(rowNo, 1);
console.log('- clicked in row ' + rowNo);
};
<a class="btn" href="#" tooltip="Delete" ng-click="deleteRow($index);">
<i class="glyphicon glyphicon-minus"></i>
</a>{{$index}}
Here is an updated plunker: http://plnkr.co/edit/7vW24aDyZH02LO1iO7F3?p=preview&s=ph0QHW513czywawl

functionality is to have a click edit a row entry, create a temporary copy of the row data, then update.
Below link
https://thinkster.io/egghead/angular-copy/

Related

How to use required in ng-show?

I have a select box in that few values which is based on the selection i need to show the labels of the textbox.
ex: if you selected
the SE means I need to show the Location2 and Reporting To label box.
If you are selecting MANAger means need to show only Location2 label text
box only. And if you are selecting ADM/cluster, any one from this means need to be display only Location alone.
Js file
$scope.ChangeRole = function(id){
roleid=$scope.Role;
if(id=="2"){
// alert(id);
$scope.showreport =true;
$scope.showreportlocation=true;
$scope.showlocation = false;
}else if(id=="3"){
$scope.showreportlocation = true;
$scope.showreport = false;
$scope.showlocation = false;
}else{
$scope.showreport = false;
$scope.showreportlocation = false;
$scope.showlocation = true;
}
}
HTML file
<div class="col-lg-6">
<div class="form-group">
<label>Role<span style="color:red">*</span></label>
<div class="input-group role">
<select placeholder="Role" ng-model="Role" class="form-control pull-right" ng-change="ChangeRole(Role);" required ng-init="selectedType()" id="selectType"" name="role" required>
<option value="0">Role</option>
<option value="1">ADM</option>
<option value="2">SE</option>
<option value="3">MANA</option>
<option value="4">ClUSTER</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group" ng-show="showlocation">
<label>Location<span style="color:red">*</span></label>
<div class="input-group location">
<select id="LocationInput" class="form-control" ng-model="LocationId" ng-options="location.Id as location.NameoftheLocation for location in locations" required>
<option id="OptionInput" value="">Location</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
<div class="col-md-6" ng-show="showreportlocation">
<div class="form-group">
<label>Location2<span style="color:red">*</span></label>
<div class="input-group site">
<select data-placeholder="Location" style="width: 100%;" ui-select2="select2Options" class="form-group" multiple ng-model="selectedTags" required>
<option ng-repeat="tag in tags" value="{{tag.Id}}">{{tag.NameoftheLocation}}</option>
</select>
<div class="input-group-addon">
<i class="fa fa-map"></i>
</div>
</div>
</div>
</div>
<div class="col-lg-6" id="otherType" ng-show="showreport">
<div class="form-group">
<label>Reporting To<span style="color:red">*</span></label>
<div class="input-group">
<select class="form-control" id="reportingto" name="reportingto" ng-options="n.Id as n.Name for n in names" ng-model="ReportingTo" required>
<option value="">Reporting To</option>
</select>
<div class="input-group-addon">
</div>
</div>
</div>
</div>
Here I have given the required option for all but which is not allowing me to save because required is present.
Using ng-show for particular selection I hide that label, but while saving html will check that field also, which is wrong please let me know that how to do.
Please some make the fiddle and let me know,
Use ng-required instead and only require it when it is shown.
I also tested Casey's suggestion of using ng-if instead of ng-show and that works as well. https://plnkr.co/edit/eeKvz5zRFeuLSF6fDbuy?p=preview

Angular JS reloading whole body when clicked on Other Tabs

My web page has 3 bootstrap tabs with data some data being populated run time. After page loads, tab1 looks fine and when I click on other tabs, whole body reloads and tab1 displays. I do not see any errors in Console.
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
<script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<div class="container" -ng-controller="incidentSummaryCtrl" >
<div class="panel panel-primary">
<div class="panel-heading"><h3>Incident {{c.data.incidentData[0].incidentId}} </h3> </div>
<div class="panel-body" style="padding:0px">
<div class="panel-group">
<div class="container">
<div class="row text-center">
<h1 class="white"></h1>
</div>
<div class="row">
<div class="col-md-10">
<div class="tab" role="tabpanel">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Incident Information</li>
<li role="presentation" class="">Potential Indicators</li>
<li role="presentation" class="">Course of Action</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade active in" id="Section1">
<div class="col-md-3 col-xs-3">
<p><label>First Name: </label> {{c.data.incidentData[0].firstName}} </p>
<p><label>Last Name: </label> {{c.data.incidentData[0].lastName}} </p>
<p><label>Email: </label> {{c.data.incidentData[0].email}} </p>
<p><label>Telephone Number: </label> {{c.data.incidentData[0].telephoneNumber}} </p>
<p><label>Reported Date: </label> {{c.data.incidentData[0].createdDate}} </p>
<p><label>Last Updated: </label> {{c.data.incidentData[0].lastUpdated}} </p>
</div>
<div class="col-md-3 col-xs-3">
<p><label>Incident #: </label> {{c.data.incidentData[0].incidentId}} </p>
<p><label>Status: </label> {{c.data.incidentData[0].incidentStatus}} </p>
<p><label>Category: </label> {{c.data.incidentData[0].incidentType}} </p>
<p><label>Organization: </label> {{c.data.incidentData[0].organization}} </p>
<p><label>Department: </label> {{c.data.incidentData[0].department}} </p>
<p><label>Assigned To: </label> {{c.data.incidentData[0].assignedTo}} </p>
</div>
<div class="col-md-12 col-xs-12">
<p><label for="notes">Notes:</label> </p>
<textarea class="form-control" rows="5" id="notes" ng-readonly="true">{{c.data.incidentData[0].note}}</textarea>
</div>
</div> <!-- End of Section1 -->
<div role="tabpanel" class="tab-pane fade" id="Section2">
<label>Extracted Potential Indicators</label>
<table class="table table-condensed table-hover table-bordered">
<thead>
<tr>
<th>No</th>
<th>Ranking</th>
<th>Indicator</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="indicator in c.data.indicatorsData track by $index" >
<td>{{indicator.number}}</td>
<td>{{indicator.urgencyRanking}}</td>
<td>{{indicator.indicatorText}}</td>
<td class="{{indicator.cssClass}}">{{indicator.indicatorStatus}}</td>
</tr>
</tbody>
</table>
</div> <!-- End of Section2 -->
<div role="tabpanel" class="tab-pane fade" id="Section3">
<p><label>Recommendation: </label> {{c.data.courseOfActionsData[0].recommendedName}} </p>
<p><label>Reasoning: </label> {{c.data.courseOfActionsData[0].reasoning}} </p>
<p><label>Select Course of Action: </label>
<select class="form-control" id="sel1" ng-model="selectedValue" style="max-width:700px">
<option ng-repeat="coa in c.data.ListOfCourseOfActionsData track by $index" value="{{coa.name}}" ng-selected="{{defaultvalue == c.data.courseOfActionsData[0].recommendedName}}">{{coa.name}}</option>
</select> </p>
<p><label>Additional Actions: </label> </p>
<div id="actions">
<label class="checkbox-inline"><input type="checkbox" value="" id="sendEmailUpdateToReporter">Send Email Update to Reporter</label>
<label class="checkbox-inline"><input type="checkbox" value="" id="notifyIsp" ng-model="notifyIsp">Notify ISP</label>
<label class="checkbox-inline"><input type="checkbox" value="" id="notifyDomainRegistrar" ng-model="notifyDomainRegistrar">Notify Domain Registrar</label> <br/>
<label class="checkbox-inline"><input type="checkbox" value="" id="sendEmailReceipt">Send Email Receipt to Reporter</label>
<label class="checkbox-inline"><input type="checkbox" value="" id="setStatusOfTicketToResolved">Set Status of Ticket Resolved
</label>
<label class="checkbox-inline"><input type="checkbox" value="" id="notifyProduction">Notify Production</label><br/>
<label class="checkbox-inline"><input type="checkbox" value="" id="resolveAndCloseTicket">Resolve and Close Ticket</label>
<label class="checkbox-inline"><input type="checkbox" value="" ng-model="checked" id="assignToGroup">Assign to Group</label>
<br/>
</div> <br/>
<div class="row">
<div id="assignment" class="col-md-4">
<b>Group Name:</b>
<select id="groupSelectedValue" ng-model="groupSelectedValue" style="max-width:700px" class="form-control">
<option value="Tier1">Tier1</option>
<option value="Tier2">Tier2</option>
<option value="Tier3">Tier3</option>
<option value="Tier4">Tier4</option>
<option value="Tier5">Tier5</option>
<option value="Halo Administrator">Halo Administrator</option>
</select>
</div>
<div class="col-md-4">
<b>Assign To:</b>
<select id="assignTo" ng-model="assignTo" style="max-width:700px" class="form-control" >
<option ng-repeat="user in (c.data.groupUsers | filter: {'groupName':groupSelectedValue}) track by $index" value="user.name">{{user.name}}</option>
</select>
</div>
</div>
<br/>
<div id="commentsSection" >
<div class="">
<span ng-hide="c.data.incidentData[0].incidentStatus == 'Closed'">
<b>Comments:</b> <textarea id="comments" class="form-control" ng-model="comments"> </textarea> <br/>
</span>
</div>
<br/>
<div class="">
<b>Comments History: </b>
<table class="table table-responsive">
<thead>
<tr>
<th>Comment </th>
<th>Entered Date </th>
<th>Entered By </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="text in c.data.commentshistory track by $index">
<td>{{text.comment}}</td>
<td>{{text.enteredDate}}</td>
<td>{{text.enteredBy}}</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" ng-click="execute()">Execute</button>
<input type="hidden" id="sysId" value="{{sysId}}">
</div>
</div>
</div> <!-- End of Section3 -->
</div>
</div>
</div>
</div>
</div> <!-- End of Container -->
</div> <!--End of Panel Group -->
</div>
</div>
</div>
Angular JS Code:
(function()
{
"use strict";
var app=angular.module('indicatorModal',[]);
app.controller('incidentSummaryCtrl',['$scope',function($scope)
{
$scope.updateSelectedCoa=function()
{
//get Values from Form
var selectedCoa=jQuery("#sel1 option:selected").text();
var sendEmailUpdateToReporter=jQuery("#sendEmailUpdateToReporter").is(":checked");
var notifyIsp=jQuery("#notifyIsp").is(":checked");
var notifyDomainRegistrar=jQuery("#notifyDomainRegistrar").is(":checked");
var sendEmailReceipt=jQuery("#sendEmailReceipt").is(":checked");
var setStatusOfTicketToResolved=jQuery("#setStatusOfTicketToResolved").is(":checked");
var notifyProduction=jQuery("#notifyProduction").is(":checked");
var assignToNextTier=jQuery("#assignToNextTier").is(":checked");
var assignToGroup=jQuery("#assignToGroup").is(":checked");
var resolveAndCloseTicket=jQuery("#resolveAndCloseTicket").is(":checked");
var sysId=jQuery("#sysId").val();
var gr=new GlideRecord('x_19668_halo_incident');
gr.addQuery('sys_id',sysId);
gr.query(callbackFunction);
function callbackFunction(gr)
{
if(gr.next())
{
$scope.updateSelectedSysIdOfCoa(selectedCoa,sysId);
gr.send_email_update_to_reporter=sendEmailUpdateToReporter;
gr.notify_isp=notifyIsp;
gr.send_email_receipt_to_reporter=sendEmailReceipt;
gr.set_status_to_resolved=setStatusOfTicketToResolved;
gr.notify_production=notifyProduction;
gr.notify_domain_registrar=notifyDomainRegistrar;
gr.assign_to_tier=assignToNextTier;
gr.assign_to_group=assignToGroup;
gr.resolve_and_close_the_ticket=resolveAndCloseTicket;
gr.update();
}
}
};
$scope.updateSelectedSysIdOfCoa=function(selectedCoa,sysId)
{
var selectedCoaSysId='';
var gr1=new GlideRecord('x_19668_halo_selcted_ticket_course_of_action');
gr1.addQuery('incident_id',sysId);
gr1.query(callbackFunction2);
function callbackFunction2(gr1)
{
if(gr1.next())
{
var gr2=new GlideRecord('x_19668_halo_courseofactions');
gr2.addQuery('ticket_coa_name',selectedCoa);
gr2.query();
if(gr2.next())
{
selectedCoaSysId=gr2.sys_id+'';
}
gr1.ticket_coa_selected_id=selectedCoaSysId;
gr1.update();
}
}
};
$scope.passSysId=function(indicatorSysId,indicatorType)
{
var ipIndicatorsData = JSON.parse(sessionStorage.getItem('ipIndicatorsData'));
var domainIndicatorsData = JSON.parse(sessionStorage.getItem('domainIndicatorsData'));
var hashCodeIndicatorsData = JSON.parse(sessionStorage.getItem('hashCodeIndicatorsData'));
if(indicatorType == 'IP Address')
{
for(var indicator in ipIndicatorsData )
{
if(ipIndicatorsData[indicator].sysId == indicatorSysId)
{
jQuery("#indicatorId").text(ipIndicatorsData[indicator].indicatorId);
jQuery("#indicatorStatus").text(ipIndicatorsData[indicator].indicatorStatus);
jQuery("#indicatorType").text(ipIndicatorsData[indicator].indicatorType);
jQuery("#urgencyRanking").text(ipIndicatorsData[indicator].urgencyRanking);
jQuery("#completedEnrichCount").text(ipIndicatorsData[indicator].completedEnrichCount);
jQuery("#enrichStartTimestamp").text(ipIndicatorsData[indicator].enrichStartTimestamp);
jQuery("#enrichEndTimestamp").text(ipIndicatorsData[indicator].enrichEndTimestamp);
jQuery("#indicatorText").text(ipIndicatorsData[indicator].indicatorText);
$("#indicatorLink").attr("href", "https://dev21310.service-now.com/haloportal/?id=potential_indicator_summary&indicator_id="+indicatorSysId);
}
}
}
else if(indicatorType == 'Full Qualified Domain Name (FQDN)')
{
for(var indicator in domainIndicatorsData )
{
if(domainIndicatorsData[indicator].sysId == indicatorSysId)
{
jQuery("#indicatorId").text(domainIndicatorsData[indicator].indicatorId);
jQuery("#indicatorStatus").text(domainIndicatorsData[indicator].indicatorStatus);
jQuery("#indicatorType").text(domainIndicatorsData[indicator].indicatorType);
jQuery("#urgencyRanking").text(domainIndicatorsData[indicator].urgencyRanking);
jQuery("#completedEnrichCount").text(domainIndicatorsData[indicator].completedEnrichCount);
jQuery("#enrichStartTimestamp").text(domainIndicatorsData[indicator].enrichStartTimestamp);
jQuery("#enrichEndTimestamp").text(domainIndicatorsData[indicator].enrichEndTimestamp);
jQuery("#indicatorText").text(domainIndicatorsData[indicator].indicatorText);
jQuery("#indicatorLink").attr("href", "https://dev21310.service-now.com/haloportal/?id=potential_indicator_summary&indicator_id="+indicatorSysId);
}
}
}
else if(indicatorType == 'Hash Code')
{
for(var indicator in hashCodeIndicatorsData )
{
if(hashCodeIndicatorsData[indicator].sysId == indicatorSysId)
{
jQuery("#indicatorId").text(hashCodeIndicatorsData[indicator].indicatorId);
jQuery("#indicatorStatus").text(hashCodeIndicatorsData[indicator].indicatorStatus);
jQuery("#indicatorType").text(hashCodeIndicatorsData[indicator].indicatorType);
jQuery("#urgencyRanking").text(hashCodeIndicatorsData[indicator].urgencyRanking);
jQuery("#completedEnrichCount").text(hashCodeIndicatorsData[indicator].completedEnrichCount);
jQuery("#enrichStartTimestamp").text(hashCodeIndicatorsData[indicator].enrichStartTimestamp);
jQuery("#enrichEndTimestamp").text(hashCodeIndicatorsData[indicator].enrichEndTimestamp);
jQuery("#indicatorText").text(hashCodeIndicatorsData[indicator].indicatorText);
$("#indicatorLink").attr("href", "https://dev21310.service-now.com/haloportal/?id=potential_indicator_summary&indicator_id="+indicatorSysId);
}
}
}
};
}]);
})();
I would suggest you to use Angular UI instead of bootstrap
https://angular-ui.github.io/bootstrap/

$scope.watch doesn't work in ionic framework

$scope.watch doesn't work in ionic framework
I was designing some fliter in ionic..It won't work ..the same code is working properly on blank test app as well as angularjs...
My html
<div class="bar bar-header bar-royal">
<div class="h1 title">Hotel search</div>
</div>
<ion-view>
<ion-content>
<h4 style="margin-top:100px;color:#116262;margin-left:10px;font-weight:bold"><i class="icon ion-edit"></i> Request Form</h4>
<div class="list" style="margin-top:15px;">
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
City
</div>
<select ng-model="formData.city">
<option value="Chennai">Chennai</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Goa">Goa</option>
</select>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">CheckIn</span>
<input type="date" ng-model="formData.checkIn" value="{{formData.checkIn | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label" style="font-weight:bold">checkOut</span>
<input type="date" ng-model="formData.checkOut" value="{{formData.checkOut | date:'yyyy-MM-dd'}}">
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Rooms
</div>
<select ng-model="formData.room">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<label class="item item-input item-select">
<div class="input-label" style="font-weight:bold">
Adults
</div>
<select ng-model="formData.adult">
<option value="1" ng-selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
<div class="row center" style="margin-top:20px;">
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
</div>
{{formData}}
<input type="text" ng-model="useStars"/>
{{fooChanges}}
</div>
</ion-content>
</ion-view>
My controller is
.controller('PlaylistsCtrl', function($scope,$state) {
$scope.formData ={};
$scope.formData.checkIn = new Date();
$scope.formData.checkOut= new Date();
$scope.formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
$scope.formData.room = 1;
$scope.formData.city = 'Chennai';
$scope.formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
})
what's problem on above code ?? ..I tried lot of times..it won't wrk..if have any alternative idea's??
As said in comment the problem was resolved doing the following :
<input type="text" ng-model="$parent.useStars"/>
However the best way to never have this problem : always use an intermdediary object like $scope.data.userStarts. So you won't ever need the $parent trick, this is called dot notation in Angular and is due to limit of javascript inheritance.
In your button you are passing the formData as parameter
<button class="button button-outline button-block button-royal" ng-click="submit(formData)" style="text-align:center;">
Search
</button>
in your controller there is not formData parameter is refereed except in your search button in the same way you should pass the useStars .
.controller('PlaylistsCtrl', function($scope,$state) {
formData ={};
formData.checkIn = new Date();
formData.checkOut= new Date();
formData.checkOut.setDate($scope.formData.checkOut.getDate() + 1);
formData.room = 1;
formData.city = 'Chennai';
formData.adult = 1;
$scope.useStars = 'balakumaran';
$scope.fooChanges = 1;
$scope.$watch(function () {
return {
useStars : $scope.useStars,
$state.go('app.search',{formData:formData});
}
},function (value) {
alert('');
},true);
$scope.submit = function(formData){
$state.go('app.search',{formData:formData});
}
});
Here you don't need $scope before form angularjs itself create a scope variable if some event occurs.
but there is no event is occurring so there is no way for PlaylistCtrl can understand $scope.formData so try to make an event to be appeare or try to understand the $scope.
so that will help for two way databinding

watching on the select item in Angularjs

I am having a div which contains a set of dropdown to select a criteria. We can add or delete criteria using the plus and minus buttons. The values to fill in the dropdown is fetched from an API.
The code for the UI is below:
<form novalidate="" role="form" name="filterForm" class="form-inline">
<div ng-repeat="criteria in criterias">
<div class="m-b">
<div class="form-group s-b" style="width: 150px;">
<span>Country</span>
<select class="form-control" ng-options="item for item in country" name="account" ng-model="criteria.country" style="max-width:100%"></select>
</div>
<div class="form-group s-b" style="width: 150px;">
<span>State</span>
<select ng-options="item for item in state" class="form-control" name="account" ng-model="criteria.state" style="max-width:100%"></select>
</div>
<div class="form-group s-b" style="width: 150px;">
<span>City</span>
<select class="form-control" ng-options="item for item in city" name="account" ng-model="criteria.city" style="max-width:100%;"></select>
</div>
<div class="form-group s-b" style="width: 150px;">
<span>Predicate</span>
<select class="form-control" name="account" ng-model="criteria.predicate" style="max-width:100%">
<option value="matches">Matches</option>
<option value="not-matches">Not Matches</option>
</select>
</div>
<div class="form-group s-b" style="width: 100px;">
<span>Value</span>
<select class="form-control" ng-options="item for item in value" name="account" ng-model="criteria.value" style="max-width:100%;"></select>
</div>
<div class="form-group s-b" style="margin-top: 20px;">
<span>
<button class="btn btn-sm btn-primary pad-btn" type="submit" ng-click="addCriteria()">
<i class="fa fa-plus"></i>
</button>
<button class="btn btn-sm btn-danger pad-btn" type="submit" ng-click="deleteCriteria(criteria)">
<i class="fa fa-minus"></i>
</button>
</span>
</div>
</div>
</div>
<button class="btn btn-sm btn-primary pad-btn align-center" type="submit" ng-click="searchParams(filterForm)">Submit
<i class="fa fa-check"></i>
</button>
<button class="btn btn-sm btn-warning pad-btn align-center" type="submit" ng-click="resetFilter()">Reset
<i class="fa fa-reply"></i>
</button>
</form>
I need to watch for the dropdown and handle the event for the corresponding criteria and update the other corresponding dropdown elements for that criteria. Please let me know how to handle the $watch element when there are many criterias when user has added using the plus button as I am not advanced programmer on AngularJs.
Updated Question
Based on the suggestion I have used the ng-change instead of $watch and I am now able to handle the event and also get the reference to the criteria object. After handling the event, I need to update the state value for the selected country. I have updated the Plnkr link with the code below but the state drop down is not getting updated even after updated the scope using $scope.criterias[index] = user;. Please let me know if I am missing something.
Updated code to handle the change event:
$scope.handleClassification = function(index){
var user = $scope.criterias[index];
user.config=["California"];
$scope.criterias[index] = user;
console.log(user);
}
I am providing the updated plnkr link for more details - Plnkr
Try this way..I guess you want to update select in a cascade way...
Your HTML file:
<div ng-controller="StaticCtrl">
<h1>Static - Oriented</h1>
<p>This approach may be better when you have the entire dataset</p>
<div>
Country:
<select id="country" ng-model="cities" ng-options="country for (country, cities) in countries" ng-change="onChange()">
<option value=''>Select</option>
</select>
</div>
<div>
State: <select id="city" ng-disabled="!cities" ng-model="suburbs" ng-options="city for (city, suburbs) in cities"><option value=''>Select</option></select>
</div>
<div>
City: <select id="suburb" ng-disabled="!suburbs" ng-model="suburb" ng-options="suburb for suburb in suburbs"><option value=''>Select</option></select>
</div>
</div>
and your JS:
function StaticCtrl($scope) {
$scope.countries = {
'India': {
'UP': ['Noida', 'Lucknow', 'Agra'],
'Maharashtra': ['Mumbai']
},
'USA': {
'San Francisco': ['SOMA', 'Richmond', 'Sunset'],
'Los Angeles': ['Burbank', 'Hollywood']
},
'canada': {
'People dont live here': ['igloo', 'cave']
}
};
$scope.onChange = function() {
console.log("Asdasd");
$scope.suburb = '';
}
}
Here is the fiddle..

select2 not working inside angularjs tabs

i have angularjs tab and i need select2 inside one of them.select2 woking in all of my page but not inside tab.
parts of tab code is:
<script type="text/ng-template" id="MetaIndicators.tpl.html" >
<div class="row col-xs-12" style=" margin-right:10px;margin-top:22px" >
<div class="form-group inline col-xs-12">
<div class=" form-group inline meta-form-groups col-xs-12">
<label class="metaLabels">ژانر</label>
<textarea name="Genre" data-kind="1" type="text" class=" form-control inline" ng-model="indicators1" ng-bind="indicators1" style="width:400px;float:right" readonly/>
<button class="btn btn-danger inline" style="margin-right: 35px;margin-top: 8px;" data-toggle="modal" data-target="#send-modal-IndicatorsGenre" ng-show="AllowEdit">ویرایش</button>
</div>
</div>
<div class="modal fade" id="send-modal-IndicatorsGenre" role="dialog" aria-hidden="true">
<div class="modal-dialog" style="background-color: white; margin-top: 100px;height: 179px; width:680px;">
<div class="modal-body" style="height:80%">
<form id="metaIndicatorsGenreInsert" class="form-horizontal" role="form">
<div class="col-xs-12" style="margin-top:8px;">
<label for="GenreID" class="col-xs-1 control-label" style="width:59px">ژانر</label>
<div class="col-xs-10">
<select multiple id="e1" style="width:500px">
<option></option>
<option ng-repeat="gn in GenreID" value="{{ gn.Key }}" ng-model="gn.Value">{{ gn.Value }}</option>
</select>
</div>
</div>
<hr class="stylish" />
<div class="col-xs-12" style="margin-top:20px;">
<i class="fa fa-mail-forward"></i> ثبت
انصراف
</div>
</form>
</div>
</div>
</div>
</script>
i add select2.min.js and select2.min.css to index .
function of fill select is :
function GetGenreListData() {
var Genre = metaService.CallService('get', "basic/GenreID", "");
Genre.success(function (data) {
$scope.GenreID = data;
}).error(function (data, status) {
alert($cookies.get('searchedMeta') + data.status);
});
}
and function of setting select2 is :
$("#e1").select2({
closeOnSelect: false
});
thanks alot

Resources