Issue
I return a array object data Ajax success result then i print li tag but its show undefind
My Ajax code is
$.ajax({
'method': 'GET',
'url': base_url +'party/selectCities?id='+state,
success: function(data) {
var newData = data.replace(/\"/g, "")
if(newData == ""){
}else{
var datas = JSON.stringify(newData);
var jsdata = JSON.parse(datas);
alert(jsdata);
var html = ``;
for(var i = 0; i<jsdata.length; i++){
html += '<li ng-click="selectcityclubs(' + jsdata[i].city+ ');>' + jsdata[i].city+ '</li>';
}
$("#ClubCity").html(html);
}
}
});
here i alert the jsdata i get result like
[{city:North Goa},{city:South Goa}]
but the li list show undefind, How to solve this issue..
please help me to solve this issue
Solution
It looks like you are missing a double quote after the ng-click
. Check out this snippet below.
Also utilizing the template literals (backticks) instead of using +
to concatenate strings helps make things more readable.
EDIT: Updated with Angular and showing how it can be done with ng-repeat
versus plain HTML
and jQuery
// angular code
angular.module('app', [])
.controller('TestController', function($scope) {
$scope.selectcityclubs = function(c) {
alert(c);
}
$scope.jsdata = [{
city: 'North Goa'
}, {
city: 'South Goa'
}];
});
// jQuery code
let jsdata = [{
city: 'North Goa'
}, {
city: 'South Goa'
}];
let html = '';
for (var i = 0; i < jsdata.length; i++) {
html += `<li ng-click="selectcityclubs('${jsdata[i].city}');">${jsdata[i].city}</li>`;
}
$("#ClubCity").html(html);
li,
button {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body ng-app="app" ng-controller="TestController">
<h2>Uses jQuery (working)</h2>
<ul id="ClubCity"></ul>
<h2>Click Works</h2>
<ul id="ClubCity1">
<li><button ng-click="selectcityclubs('North Goa')">North Goa</button></li>
<li><button ng-click="selectcityclubs('South Goa')">South Goa</button></li>
</ul>
<h2>Uses ng-repeat (working)</h2>
<ul id="ClubCity2">
<li ng-repeat="city in jsdata" ng-click="selectcityclubs(city.city);">{{city.city}}</li>
</ul>
</body>
Answered By - treckstar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.