Adro Adro - 1 year ago 60
AngularJS Question

Two-Way Data Bind Not Applying with Controller - AngularJS

Good morning S.O. Gurus. Im having difficulty getting the two-way data binding working. The two way data binding is working in every other controller no problem, but this one is giving me a hard time. Can you assist please?

Controller / HTML:



app.controller('search', function($scope, $http, $location) {
$scope.searchText = "";
$scope.searchRun = function() {


$scope.userQuery = "Last::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+First::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+Display::Name+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+User::Login+eq+"+$scope.searchText.replace(/ /g,"::")+"+or+Manager::Display::Name+eq+"+$scope.searchText.replace(/ /g,"::");

$http({
method: 'GET',
url: url
})
.then(function(res) {

$scope.TransmutedUsers = {
users: [

]
};
//Looping through users making a new object
for(var i = 0; i < res.data.users.length; i++) {
//console.log(res.data.users[i].id);
var obj = {
id: res.data.users[i].id
};

for(var x = 0; x < res.data.users[i].fields.length; x++) {
//console.log(res.data.users[i].fields[x].name);
var nameVar = res.data.users[i].fields[x].name.replace(/\s+/g, '');
var valueVar = res.data.users[i].fields[x].value;

obj[nameVar] = valueVar;

}
$scope.TransmutedUsers.users.push(obj);
}


console.log($scope.TransmutedUsers.users);
$scope.returnedUsersLength = $scope.TransmutedUsers.users.length;
console.log($scope.returnedUsersLength);
sessionStorage.setItem("searchActive", "true");


if ($location.url() != "/search") {
$location.url("/search");
}

});
};
});

<nav class="navbar navbar-default navbar-fixed-top bg-color--brand-blue">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a class="navbar-brand" href="#"><img src="/images/logo.png" class="logo" alt=""></a></li>
<li><a href="/dashboard"><img src="/images/eAccess-logo-header.png" width="80" alt=""></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="/profile">Welcome, <span>{{userName}}</span></a></li>
<li><a href="/profile">Contact Help</a></li>
<li><a href="/profile">Close Window</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="sub-header bg-color--light-grey">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-sm-3"><a href="/newrequest" class="btn btn-primary btn-sm">Make a Request</a></div>
<div class="col-sm-2 padding-right-none"><a href="/myprofile">My Profile</a></div>
<div class="col-sm-2 padding-left-none"><a href="/directs">My Direct Reports</a></div>
<div class="col-sm-2"><a href="/requests">My Requests</a></div>
</div>
</div>
<div class="col-sm-6">
<form ng-controller="search" ng-submit="searchRun();" class="pull-right">
<input type="text" placeholder="Search for people or requests" ng-model="searchText">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
</div>
</div>

<div class="container" id="Search" ng-controller="search">
<div class="row" id="SearchResultsUsers">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-12">
<h1>Search Results: <i>{{searchText}}</i></h1>
</div>
</div>
<div class="row" ng-repeat="user in TransmutedUsers.users">
<div class="col-sm-12">
<h3><i class="fa fa-user-circle"></i> <strong>{{user.FirstName}}</strong> {{user.LastName}} <small>({{user.UserLogin}})</small> <span>{{user.Role}}</span></h3>
</div>
<div class="col-sm-12">
<p>{{user.JobTitle}}, <a href="mailto:{{user.Email}}" ng-show="user.Email">{{user.Email}}</a> <span ng-show="user.Phone">{{user.Phone}}</span> Reports to: {{user.ManagerDisplayName}}</p>
</div>
<div class="col-sm-12">
<p>Direct Reports: <span ng-show="user.DirectReports">{{user.DirectReports}}</span><span ng-show="!user.DirectReports">None</span></p>
</div>
</div>
</div>
</div>
</div>





searchText does console.log but it does not bind to the {{searchText}} and the url does return the object successfully in the format specified, but that doesn't work with the {{}} either.

Using AngularJS 1.6

Please let me know if other information is required to help me solve this conundrum. Thank you all!

Answer Source

After declaring the $scope.searchText outside the search function,

you can force it by replacing the form :

<form ng-controller="search" ng-submit="searchRun();" class="pull-right">
                    <input type="text" placeholder="Search for people or requests" ng-model="searchText">
                    <button type="submit"><i class="fa fa-search"></i></button>
                </form>

with something easy like:

<input type="text" placeholder="Search for people or requests" ng-model="searchText">
 <button ng-click="searchRun(searchText)"><i class="fa fa-search"></i></button>

and inside your search function:

$scope.searchRun = function(searchText) {
        $scope.searchText = searchText;
        ...rest of ur code

You can also try to debug this using this, in otder to see the changes made in the input directly in the html.

<input type="text" ng-model="searchText">
{{searchText}}

and in your controller declare outside the function

$scope.searchText = "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download