I would like fire a search after my user finish write (without a enter) in angularjs.
My html (simplified):
<div ng-class="input-append" ng-controller="searchControl">
<input type="text" ng-model="ajaxSearch" ng-change="search();">
</div>
My AngularJs (simplified):
$scope.searchControl = function() {
$scope.search = function(){
$http({
method: 'POST',
url: '<?php echo base_url('system/ajax_search/') ?>',
'data: search=' + $scope.ajaxSearch,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(data) {
$scope.result = data;
});
}
The original code is extensive, so i simplified.
In my code, i post data always my user change the search.
I would like post data seconds after my user stop to write.
Any ideas?
This can be easily achieved with a directive:
angular.module('myApp', [])
.directive('keyboardPoster', function($parse, $timeout){
var DELAY_TIME_BEFORE_POSTING = 3000;
return function(scope, elem, attrs) {
var element = angular.element(elem)[0];
var currentTimeout = null;
element.onkeypress = function() {
var model = $parse(attrs.postFunction);
var poster = model(scope);
if(currentTimeout) {
$timeout.cancel(currentTimeout)
}
currentTimeout = $timeout(function(){
poster();
}, DELAY_TIME_BEFORE_POSTING)
}
}
})
.controller('testController', function($scope){
$scope.search = function() {
console.log("Executing query...");
}
})
And it can be used like this...
<div ng-app='myApp' ng-controller='testController'>
<input type="text" keyboard-poster post-function="search">
</div>
Use $timeout and cancel each time user types; if the timeout runs, executes the scoped function given as an attr. You can modify the delay time to whatever fits better your user experience (I wouldn't drop it below 1000 though).
Related
I want to refresh the method $scope.getPIRData dynamically according to the text box value , I have one text box where I can give some seconds like 3000 ms , that I need to get into the setInterval block but my text box values not setting to the window.refreshtime.
Method is refreshing properly but after selecting the dropdown list the refresh mechanism not working before selecting the dropdown only it is working fine.
html
<input type="number"
ng-model="refreshtime"
ng-model-options="{ updateOn: 'blur' }"
ng-change="setupInterval()"
id="txtRefresh" name="name" />
<select class="form-control ng-pristine ng-valid ng-scope ng-empty ng-touched" ng-model="sel_val" ng-change="getPIRData(sel_val.deveui)" ng-options="data.details for data in pirs">Select PIR Device</select>
Java script
var app = angular.module('PIR_Detection', []);
app.controller('myCtrl', function ($scope, $http, $window, $timeout) {
$scope.sel_val = 0;
$scope.DefaultLabel = "Loading.....";
$scope.refreshtime = 1000;
var post = $http({
method: "get",
url: "../data.json",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
$scope.pirs = data;
});
post.error(function (data, status) {
});
$scope.getPIRData = function (id) {
var url = "/PIRDetails/GetPIRStatus/" + id;
$http.get(url)
.then(function (response) {
$scope.myWelcome = response.data;
if ($scope.myWelcome != "") {
$scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
}
$window.deviceId = id;
})
// next call will be made after the request
.finally($scope.setupInterval);
};
let timeOut = null;
$scope.refreshPIR = function () {
if (timeOut) {
// removes the previous timeout from event loop
$timeout.cancel(timeOut);
}
console.log('timeout method call at ', $scope.refreshtime, 'ms');
timeOut = $timeout(function () {
if ($window.deviceId) {
$scope.getPIRData($window.deviceId);
} else {
$scope.refreshPIR();
}
}, $scope.refreshtime);
};
//init
$scope.refreshPIR();
});
use setTimeout over setInterval to get more control over the execution (https://weblogs.asp.net/bleroy/setinterval-is-moderately-evil).
AngualrJs has inbuilt $timeout service, which takes care of the digest cycle.
var app = angular.module('PIR_Detection', []);
app.controller('myCtrl', function ($scope, $http, $window, $timeout) {
$scope.sel_val = 0;
$scope.DefaultLabel = "Loading.....";
$scope.refreshtime = 1000;
// commenting the data code, just for the solution demo
/* var post = $http({
method: "get",
url: "../data.json",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
});
post.then(function (data, status) {
$scope.pirs = data;
});
post.catch(function (data, status) {
}); */
$scope.getPIRData = function (id) {
var url = "/PIRDetails/GetPIRStatus/" + id;
$http.get(url)
.then(function (response) {
$scope.myWelcome = response.data;
if ($scope.myWelcome != "") {
$scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
}
$window.deviceId = id;
})
// next call will be made after the request
.finally($scope.refreshPIR);
};
let timeOut = null;
$scope.refreshPIR = function() {
if(timeOut) {
// removes the previous timeout from event loop
$timeout.cancel(timeOut);
}
console.log('timeout method call at ',$scope.refreshtime, 'ms');
timeOut = $timeout(function() {
if($window.deviceId) {
$scope.getPIRData($window.deviceId);
} else {
$scope.refreshPIR();
}
}, $scope.refreshtime);
};
//init
$scope.refreshPIR();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="PIR_Detection" ng-controller="myCtrl">
Refresh interval:
<!-- Used ng-model-options to fire the change event once user comes out the textbox-->
<input type="number"
ng-model="refreshtime"
ng-model-options="{ updateOn: 'blur' }"
ng-change="refreshPIR()"
id="txtRefresh" name="name"/>
</div>
Ok so two things... first of all, note the misspelling in $scope.refrestime.
That being said, this won't fix your problem. setInterval's interval is set when you call it and cannot be changed.
Seeing as you seem to want to only change the interval on the next time the interval hits, What you can do is pull the function out of the setInterval and into its own reference and instead of setInterval simply use setTimeout and then as the last line of the function just call setTimeout(refreshPIR, $scope.refreshtime)
function refreshPIR() {
if (window.deviceId != null) {
$scope.getPIRData(window.deviceId);
}
setTimeout(refreshPIR, $scope.refreshtime);
}
setTimeout(refreshPIR, window.refreshtime);
Tho you might want to add some error handling to make sure scope.refreshtime is an int :-)
I need to bind some HTML code to the response of a AJAX request. Since the AJAX is asynchronous, I need to place the code that does the actual binding within my AJAX callback function, like so:
(function() {
'use strict'
angular.module('poetry', [])
.controller('poetryController', poetryController);
poetryController.$inject = ['$scope', '$sce']
function poetryController ($scope, $sce) {
$.ajax({
type: 'GET',
url: myURL,
data: {'per_page': 100},
dataType: 'json',
success: callback
});
function callback(response) {
var list = new Array
for (var i = 0; i < response.length; i++) {
var string = '<li><h3>' + response[i]['title']['rendered'] + '</h3></li>';
list.push(string)
}
$scope.html_code = list.join("")
$scope.bindHTML = function(html_code) {
return $sce.trustAsHtml(html_code)
}
};
}
})();
What ends up happening is that the HTML doesn't get sent to the page, and I'm left with empty content. I'm quite confident the problem is in this line here
$scope.bindHTML = function(html_code) {
return $sce.trustAsHtml(html_code)
}
and something to do with the scope of the $scope.bindHTML variable.
What should I do to make $scope.html_code pass to the $scope.bindHTML function?
Also, no errors show in the console.
My HTML:
<body ng-app="poetry">
<div href="#" class="container" ng-controller="poetryController">
<ul class="grid effect-6" id="grid" ng-bind-html="bindHTML(html_code)"></ul>
</div>
</body>
I'm working with an old version of AngularJS (1.3). I've got a page that I want to conditionally show different things based on the value in the database. If the value in the database is changed via user interaction, I want to update what's shown automatically. Part of what I show, however, is HTML and in that HTML I need to include some AngularJS code.
If the value is True, I want to show this HTML:
Your value is True. To set it to False, <a ng-click="myToggleFunction('paramValueFalse')">click here</a>.
If the value is False, I want to show this HTML:
You haven't done the thing, to do the thing, <a ng-click="myDifferentFunction('someOtherParamValue')">click here</a>.
I've got it so close to working: the content that shows changes out depending on what the user's value is, and it updates appropriately, and it's even rendering the HTML correctly (using $sce)... But the ng-click isn't functioning. Can you include angular in HTML that's being injected via JS like that?
Full code:
HTML:
<span ng-bind-html="renderHtml(html_content)"></span>
Controller:
function MyCtrl ($scope, $http, $sce, Notification) {
$scope.username = context.targetUsername;
$scope.content_options = {
'yes' : 'Your value is True. To set it to False, <a ng-click="myToggleFunction(" + "'paramValueFalse'" + ')">click here</a>.',
'no' : 'You haven\'t done the thing, to do the thing, <a ng-click="myDifferentFunction(" + "'someOtherParamValue'" + ')">click here</a>.'
}
$http.get(
'/api/v1/user/' + $scope.username + '/?fields=myBooleanField' // django rest api call
).then(function(response) {
$scope.user = response.data;
if ($scope.user.myBooleanField) {
$scope.html_content = $scope.content_options['yes'];
} else {
$scope.html_content = $scope.content_options['no'];
}
});
});
$scope.myToggleFunction = function(paramValue) {
// toggle value in the db
if (accepted === 'true') {
var success = "You turned on the thing";
var content = "yes";
} else {
var success = "You turned off the thing";
var content = "no";
}
$http({
method: 'GET',
url: '/api/v1/user/' + $scope.username + '/my_boolean_field/?value=' + paramValue, // django rest api call
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
$scope.html_content = $scope.content_options[content];
Notification.success(success);
}, function(response) {
Notification.error("There was an error.");
});
};
$scope.myDifferentFunction = function(someOtherParamValue) {
// do some other stuff
};
$scope.renderHtml = function(html_code) {
return $sce.trustAsHtml(html_code);
};
}
MyCtrl.$inject = ['$scope', '$http', '$sce', 'Notification'];
As Sagar said above, the reason this is happening is because the html code returned by renderHtml is not compiled by AngularJS. I tried a few different takes on creating a directive that recompiles angular. For example:
https://github.com/incuna/angular-bind-html-compile
Rendering directives within $sce.trustAsHtml
ng-click doesn't fire when added post load
However, none of these were working for me. I'm not sure why; the content just wasn't displaying but there were no JS errors.
I ended up finding this solution, and it worked for me: Angular: ng-bind-html filters out ng-click?
Essentially, the solution is use raw JS to directly call the Angular functions, rather than using the ng-click directive in the JS-generated HTML content.
Here's what it looks like:
Template:
<div id="angularHtml" ng-bind-html="html_content">
<script>
function callAngular(controllerFunction, functionParam) {
var scope = angular.element(document.getElementById('angularHtml')).scope();
scope.$apply(function() {
{# couldn't figure out how to reference the function from the variable value, so this is hacky #}
if (controllerFunction == "myToggleFunction") {
scope.myToggleFunction(functionParam);
} else if (controllerFunction == 'myDifferentFunction') {
scope.myDifferentFunction(functionParam);
}
});
}
</script>
Controller:
function MyCtrl ($scope, $http, $sce, Notification) {
$scope.username = context.targetUsername;
$scope.content_options = {
'yes' : 'Your value is True. To set it to False, <a onClick="callAngular(\'myToggleFunction\', \'false\')">click here</a>.',
'no' : 'You haven\'t done the thing, to do the thing, <a onClick="callAngular(\'myDifferentFunction\', \'someValue\')">click here</a>.'
}
$http.get(
'/api/v1/user/' + $scope.username + '/?fields=myBooleanField' // django rest api call
).then(function(response) {
$scope.user = response.data;
if ($scope.user.myBooleanField) {
$scope.html_content = $sce.trustAsHtml($scope.content_options['yes']);
} else {
$scope.html_content = $sce.trustAsHtml($scope.content_options['no']);
}
});
});
$scope.myToggleFunction = function(paramValue) {
// toggle value in the db
if (accepted === 'true') {
var success = "You turned on the thing";
var content = "yes";
} else {
var success = "You turned off the thing";
var content = "no";
}
$http({
method: 'GET',
url: '/api/v1/user/' + $scope.username + '/my_boolean_field/?value=' + paramValue, // django rest api call
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
$scope.html_content = $sce.trustAsHtml($scope.content_options[content]);
Notification.success(success);
}, function(response) {
Notification.error("There was an error.");
});
};
$scope.myDifferentFunction = function(someOtherParamValue) {
// do some other stuff
};
}
MyCtrl.$inject = ['$scope', '$http', '$sce', 'Notification'];
You can use ngShow and ng-hide for show and hide HTML dynamic
<div ng-show="DBvalue">Your value is True. To set it to False, <a ng-click="myToggleFunction('paramValueFalse')">click here</a>.</div>
<div ng-hide="DBvalue">You haven't done the thing, to do the thing, <a ng-click="myDifferentFunction('someOtherParamValue')">click here</a>.</div>
I have a lot of pages, where i use $http to process requests (get data, update) and i have to use ajax-loading.gif every time.
Now, i need to do it like this:
<div ng-show="model == null || ajaxUpdating">
<div>
<img src="~/Content/Images/gif-load.gif" />
<p>Waiting server respone...</p>
</div>
</div>
Here i have ajaxUpdating flag, that i init before request and set false in success or error callbacks:
$scope.ajaxUpdating = true;
$http({
url: updateUrl,
method: 'POST'
}).
success(function (data, status) {
window.location.href = settings.redirectAfterOk;
}).
error(function (data, status) {
$scope.ajaxUpdating = false;
alert(data.errorMsg || settings.errors.update);
});
So, i wanna know, is it possible to check, if request processing right now? I don't want to use so many flags every where in my code and it could be much easier, if i just write:
$http.isProcessing
for example.
Thx you.
If you need a loading gif whenever an ajax http request is in progress, you can set an interceptor on the config like this:
var myApp = angular.module('myApp', []).config(
[ '$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
//routes here
}]).config(function($httpProvider) {
//show a loading div when a http request is running
var numLoadings = 0;
var loadingScreen = $('<div style="position:fixed;top:0;left:0;right:0;bottom:0;z-index:10000;background-color:gray;background-color:rgba(70,70,70,0.2);"><img style="position:absolute;top:50%;left:50%;" alt="" src="css/loading.gif" /></div>').appendTo($('body')).hide();
$httpProvider.responseInterceptors.push(function() {
return function(promise) {
numLoadings++;
loadingScreen.show();
var hide = function(r) { if (!(--numLoadings)) loadingScreen.hide(); return r; };
return promise.then(hide, hide);
};
});
});
See $http, look for Interceptors
I have a REST service that I made which returns a json string which is simply a set of strings (I used Gson to generate this string (Gson.toJson(mySetOfStrings))
So I have added to my index.html:
<div ng-controller="ListOptionsCtrl">
<form novalidate>
<button ng-click="refreshList()">refresh</button>
<select name="option" ng-model="form.option" ng-options="o.n for o in optionsList></select>
</form>
</div>
and in my script:
var ListOptionsCtrl = function ListOptionsCtrl($scope, $http) {
$scope.refreshList = function() {
$http({
method: 'GET'
url: '*someurl*'
}).
success(function(data) {
$scope.optionsList = angular.fromJson(data);
});
};
}
Unfortunately all this produces in my select box is an empty list. When I see what the response to the GET request is it returns a json string with content in it so I do not see why nothing is being added to this element. What am I doing wrong here? thanks
It is because Angular does not know about your changes yet. Because Angular allow any value to be used as a binding target. Then at the end of any JavaScript code turn, check to see if the value has changed.
You need to use $apply
var ListOptionsCtrl = function ListOptionsCtrl($scope, $http) {
$scope.refreshList = function() {
$http({
method: 'GET'
url: '*someurl*'
}).
success(function(data) {
$scope.$apply(function () {
$scope.optionsList = angular.fromJson(data);
});
});
};
}
Try this.
More about how it works and why it is needed at Jim Hoskins's post
You should check for $digest error by doing if(!$scope.$$phase) { ... } before doing $apply.
success(function(data) {
if(!$scope.$$phase) {
$scope.$apply(function () {
$scope.optionsList = angular.fromJson(data);
});
}
});