0

I have a simple html file that make make search on Algolia and returns result. I can console the result but can not access $scope.users from view. How can I grab that $scope.users in view.

here is my app.js file

    var myApp = angular.module('myApp', []);

    myApp.controller('usersController', function($scope) {
        $scope.users = [];

        var client = algoliasearch('my_app_id', 'my_app_key');
        var index = client.initIndex('getstarted_actors');

        index.search('john', function searchDone(err, content) {
          $scope.users.push(content.hits);
          console.log(content.hits);
        });

    });

Here is my html view file

<div class="results" ng-controller="usersController">
    <div ng-repeat="user in users">
        <h3>{{ user.name }}</h3>
    </div>  
</div>

note: ng-app="myApp" attribute given in html tag.

3
  • what does {{user}} print, I believe you don't have the name property on the user. Commented May 12, 2016 at 16:00
  • Sintax looks old, try ng-controller="usersController as user" then ng-repeat="usr in user.users" and {{usr.name}} , OBVIOUSLY be sure your $scope.users.push() is really pushing Commented May 12, 2016 at 16:01
  • I think $scope.users.push() its not really pushing. because when i console $scope.users outside of index.search function() its just consoling empty [] array. What should I do? @sbaaang? Commented May 12, 2016 at 16:08

3 Answers 3

1

It's most likely because your index.search call isn't triggering an Angular $digest cycle - manually trigger one with either $apply or a $timeout

index.search('john', function searchDone(err, content) {
  $scope.users.push(content.hits);
  $scope.$apply();
});

The $apply() could throw $digest already in progress errors - another way with a $timeout

myApp.controller('usersController', function($scope, $timeout) {
    index.search('john', function searchDone(err, content) {
        $timeout(function() {
            $scope.users.push(content.hits);
        });
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

$scope.$evalAsync() i would say :P instead of $timeout
no need for the $timeout as it wraps a call to $apply docs.angularjs.org/api/ng/service/$timeout which can be turned off by setting invokeApply to false (defaults to true)
I think $scope.users.push() its not really pushing. because when i console $scope.users outside of index.search function() its just consoling empty [] array. What should I do?
@MohammadOhidul -- Did you try the above solution? The log could be wrong since the above search is async...
0

try calling $scope.$apply() to update your bindings

index.search('john', function searchDone(err, content) {
  $scope.users.push(content.hits);
  console.log(content.hits);
  $scope.$apply();
});

Comments

0

algoliasearch doesn't look like it's an injected service and therefore not native to the angular framework. Try calling $scope.$apply().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.