简体   繁体   English

无法在AngularJs中显示$ scope对象数组

[英]Cant display $scope array of objects in AngularJs

I have this weird problem that i cant display my scope variable values. 我有一个奇怪的问题,我无法显示我的范围变量值。 I am new with angular but i have done this many times before. 我是新来的有角度的人,但是我以前做过很多次。 So here is main parts of index.html. 因此,这里是index.html的主要部分。 div-ui is inside of body but it doesn't see here: div-ui在体内,但在这里看不到:

<input type="text" class="form-control" ng-model="searchUsers" placeholder="Search users"/>
<a ng-click="search()" ui-sref="search">Search</a>

<div ui-view>

</div>

Here is search.html: 这是search.html:

<p>Hello world</p> // Shows normally
<p>{{test1}}</p> // Shows normally
<p>{{test2}}</p> // Nothing
<p ng-repeat="x in searchResult">{{x.username}}</p> // Nothing
<p ng-repeat="(key,value) in searchResult">{{value}}</p> // Nothing
<p ng-repeat="(key,value) in searchResult">{{value.username}}</p> // Nothing

Here is the controller: 这是控制器:

(function(){
angular.module('TimeWaste')
    .controller('NavigationCtrl', ["$scope", "$http", "$state",
    function($scope,$http,$state){

        $scope.searchResult = [];
        // Tried with and without this

        if(localStorage['User-Data']){
            $scope.loggedIn = true;
        }else{
            $scope.loggedIn = false;
        }

        $scope.logUserIn = function(){
            $http.post('api/user/login', $scope.login)
                .success(function(response){
                    localStorage.setItem('User-Data', JSON.stringify(response));
                    $scope.loggedIn = true;
                }).error(function(error){
                    console.log(error);
                });
        }

        $scope.logOut = function (){
            localStorage.clear();
            $scope.loggedIn = false;
        }

        $scope.test1 = "hi";

        $scope.search = function (){

             $scope.test2 = "hi again";

            $http.post("api/user/search", {username: $scope.searchUsers})
                .success(function(response){

                    $scope.searchResult = response;
                    console.log($scope.searchResult); 
         // returns array of objects. There is all information that i want.

                }).error(function(error){
                    console.log("ei");
                });
        }
    }]);
}());

Everything looks just normal. 一切看起来都很正常。 Inside of search function it's working and console.log returns just what i except. 在搜索功能内部,它可以正常工作,console.log返回除我以外的内容。 I have also tried repeat divs and tables but i am pretty sure that it's not the problem here. 我也尝试过重复div和表,但是我很确定这不是这里的问题。

Here is also my app.js if the problem is there: 如果问题出在这里,这也是我的app.js:

(function(){
angular.module('TimeWaste', ['ui.router', 'ngFileUpload'])
    .config(function ($stateProvider, $urlRouterProvider){

        $urlRouterProvider.otherwise("/");

        $stateProvider
            .state("main", {
                url: "/",
                templateUrl: "app/main/main.html",
                controller: "MainCtrl"
            })
            .state("search", {
                url: "/search",
                templateUrl: "app/main/search.html",
                controller: "NavigationCtrl"
            })
    });
}());

There is couple more states and they all works just fine. 还有几个州,它们都可以正常工作。 I made it little bit shorter so this post won't be so long. 我把它缩短了一点,所以这篇文章不会那么长。

OK, at this very point you must add an $scope.$apply or use '.then' instead of 'success' to keep your code according promise pattern. 好的,在这一点上,您必须添加$ scope。$ apply或使用'.then'而不是'success'来保证代码符合Promise模式。
When you just post, there is a need to force the digest cycle to happen. 当您刚发布时,有必要强制摘要周期发生。

$http.post("api/user/search", {username: $scope.searchUsers})
    .success(function(response){
       $scope.$apply(function() {
           $scope.searchResult = response;
        });
    }).error(function(error){
        console.log("ei");
    });

The reason you're able to see {{test1}} and not other values is because you have 2 different controllers called 'MainCtrl' and 'NavigationCtrl'. 之所以能够看到{{test1}}而不是其他值,是因为您有两个名为'MainCtrl'和'NavigationCtrl'的不同控制器。 You are using ui-sref to switch states. 您正在使用ui-sref切换状态。 So, this is what happening. 所以,这就是发生的事情。

  1. When you click your href link, it looks for search() method inside your MainCtrl and then change the state to "search". 当您单击href链接时,它将在MainCtrl中查找search()方法,然后将状态更改为“搜索”。

  2. It then loads the variables and methods from NavigationCtrl into scope and that's why you're able to see {{test1}} which is loaded into the scope. 然后,它将NavigationCtrl中的变量和方法加载到范围中,这就是为什么您能够看到{{test1}}加载到范围中的原因。 But you haven't called search() method and hence you're not able to see the other values. 但是您尚未调用search()方法,因此看不到其他值。

To check my answer, call your method explicitly inside your controller after your function definition $scope.search(); 要检查我的答案,请在函数定义$scope.search();之后在控制器内显式调用您的方法$scope.search();

If you're seeing the result then that is your problem. 如果您看到结果,那就是您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM