I've searched and searched, but perhaps I don't know the words to represent what I'm trying to do. This is my first app attempt with AngularJS and would appreciate any guidance you can provide.
I have a basic HTML page with the following:
<div ng-app="testApp">
<div ng-controller="SearchCtrl">
<input type="text" ng-model="params.firstName">
<input type="text" ng-model="params.lastName">
<h1>{{ searchString() }}</h1>
<table>
<tr ng-repeat="employee in results">
<td>{{employee}}</td>
</tr>
</table>
</div>
</div>
I have included the AngularJS library as well as my own app.js file containing the following:
var testApp = angular.module('testApp' []);
testApp.factory('Data', function() {
return {
firstName: "", lastName: ""
}
})
function SearchCtrl($scope, Data, $http) {
$scope.params = Data;
$scope.searchString = function() {
return 'fname=' + $scope.params.firstName + '&lname=' + $scope.params.lastName;
}
}
With this, my function binds properly and the code within my h1 tag updates dynamically. What I want to do is have the results of an $http get request update dynamically based on the search string. I had code like this inside the controller, which executes - but does not update each time a bound variable updates.
$http.get('results.php?' + $scope.searchString)
.then(
function(res) {
$scope.results = res.data;
});
I'm sure I'm missing a major concept / component of AngularJS. How do I trigger the $http call each time the bound variable changes?
Appreciate any assistance you can provide!
Edit
Attempted the chances proposed by @Sprottenwels, using the params object:
$http.get('results.php?', {
params: { fname: $scope.firstName, lname: $scope.lastName }
})
.then(function(res) {
$scope.results = res.data;
});
});
The call occurs without error, but both params come up blank in the Chrome console (results.php?fname=&lname=)
Edit 2
At the time the page is laoded, the following call is made:
results.php?fname=&lname=
A log of $scope shows the following relevant elements:
params: firstName: "", lastName: ""
searchString: function() { ... }
Makes sense. Editing a bound variable using the input boxes on the page updates the searchString ( properly displayed in the h1 tag using {{ searchString() }} ) - but does not make the $http call again.
console.log()your $scope properties at the very moment you are making the call and post the results?paramsobject will be concatenated to a properly workingget. Also, what is firing the call?