javascript - Angularjs $http not returning Calls on ng-click after 1 st call -
i have services use in landing controller. problem when app init $http gets called default. need give params based on click. have implmented ajax call on ng-click.
but getting same data on ng-click
var sent = angular.module("sentanalysis",["ngroute"]); var log = console.log.bind(console); 'use strict'; sent.service('sentiments', function ($http) { var sentiments = []; return { getdata: function(api,params,callback) { log(api); var cb = callback || angular.noop; if(sentiments.length !== 0) { cb(sentiments); }else{ $http.get(api, { params : params }) .success(function(result) { sentiments = result; cb(result); }) .error(function() { cb(); }) } } } });
and controller is
app.controller('landingctrl', function ($scope,$http,sentiments,$rootscope) { $scope.clickyesterday = function() { sentiments.getdata(api,params,function(res) { } }; $scope.clicktomorrow = function() { sentiments.getdata(api,params,function(res) { } }; /*default http executes*/ sentiments.getdata(api,params,function(res) { } }]);
the default gets executed functions bind ng-click not executed alwsys return same json server server written in nodejs express , felixge-node/mysql , moment.
the server working clint end not work on ng-click
the base route is
app.get('/api/sentiment',function(req,res) { var sent ={}; sent.status =false; if(user.isdb) { var startdate = req.param('startdate'); var enddate = req.param('enddate'); var startvalid = moment(startdate).isvalid(); var endvalid = moment(enddate).isvalid(); log(startvalid + " "+endvalid); if(startvalid && endvalid) { var options ={}; options.values = []; options.values.push(startdate); options.values.push(enddate); options.sql = 'some query '; options.timeout = 4000; connection.query(options,function(err, rows, fields) { if (!err) { log("data row received query"); log("data row length : "+rows.length); log("executing query :"); log("startdate : "+startdate); log("enddate : "+enddate); log(options.sql); if(rows.length!=0) { sent.status =true; sent.data = rows; } res.send(sent); } }); } else { log("invalid date format/or data error"); } } else { log("database connection issue"); res.send(sent); } });
and html this
<div ng-click="clickyesterday()" class="btn btn-primary"><b>yesterday</b></div>
the problem here sentiment object in services sentiment = [];
its assigned on startup not getting re initialized holding previous value , sending same json data on every calls. since json sotred here first time call made. removing if sentiment resolves issue
Comments
Post a Comment