I have developed AngularJS application that integrates with PouchDB database. When trying to get info from the database, the $scope variable exists only inside the method.
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
$scope.$apply(function(){
$scope.info = doc.rows;
});
});
$scope.select = function(id){
for(var i = 0; i < $scope.info.length; i++){
if(id == $scope.info[i].doc._id){
$scope.$apply(function (){
$scope.sName = $scope.info[i].doc.name;
$scope.sSurname = $scope.info[i].doc.surname;
$scope.sPhone = $scope.info[i].doc.phone;
$scope.sAddress = $scope.info[i].doc.address;
console.log($scope.info[i].doc);
});
}
}
};
Here I call the Select function to select a user, and then I want to show that user in the inputs so I can update the info.
<div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar" role="navigation">
<h3>All users</h3>
<div class="list-group">
<a href="#" ng-repeat="i in info" class="list-group-item" ng-click="select(i.doc._id)">{{i.doc.name + ' ' + i.doc.surname}}</a>
</div>
</div>
Here I use $scope variables
<div class="col-xs-6 col-sm-3 sidebar-offcanvas" style="margin-left: 20%;">
<h3>Selected user</h3>
<input type="text" ng-model="sName" class="form-control" placeholder="Name" />
<input type="text" ng-model="sSurname" class="form-control" placeholder="Surname" />
<input type="text" ng-model="sPhone" class="form-control" placeholder="Phone" />
<input type="text" ng-model="sAddress" class="form-control" placeholder="Address" />
<br/>
<table>
<tr>
<td><button ng-click="" class="btn btn-lg btn-primary">Update</button></td>
<td><label style="visibility: hidden;">a;dl</label></td>
<td><button ng-click="" class="btn btn-lg btn-danger">Remove</button></td>
</tr>
</table>
</div>
$scope.sName, $scope.sSurname... are undefined outside the select function..
Any help?