what is the use of angular.bind in angularjs? Where to use it? -
what use of angular.bind in angularjs. please provide example. cant understand https://docs.angularjs.org/api/ng/function/angular.bind
angular.bind utility function combines functionality in function.bind , partial function application.
binding (in general) idea want bind current context function, execute @ later time.
this can useful in angular when making http calls $http , handling promises:
$http.get('url').then(angular.bind(this, function(response) { this.response = response; //use (which bound context) }); in above example, this inside function not refer this in $http context unless explicitly bind it. common javascript issue (in callbacks) because of dynamic binding of context (which unlike popular class-oriented languages).
partial application used when want make function has been passed some of arguments. simple example:
function add(x, y) { return x + y; } var add10to = angular.bind(this, add, 10); console.log(add10to(5)); // outputs 15 with angular.bind, angular team giving both of these wrapped together.
Comments
Post a Comment