【问题标题】:Cant display $scope array of objects in AngularJs无法在 AngularJs 中显示 $scope 对象数组
【发布时间】:2026-02-16 13:55:01
【问题描述】:

我有一个奇怪的问题,我无法显示我的范围变量值。我是 Angular 新手,但我以前做过很多次。所以这里是 index.html 的主要部分。 div-ui 在 body 内部,但在这里看不到:

<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>

这里是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

这里是控制器:

(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");
                });
        }
    }]);
}());

一切看起来都很正常。在搜索功能内部,它正在工作,console.log 只返回我除外的内容。我也尝试过重复 div 和表格,但我很确定这不是这里的问题。

如果有问题,这也是我的 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"
            })
    });
}());

还有更多的状态,它们都工作得很好。我把它缩短了一点,所以这篇文章不会那么长。

【问题讨论】:

  • $scope.searchResult = response.data?
  • @Frost 你能在这里附上一个有帮助的示例回复
  • 试过了。没用
  • @vickyKumar 这里是示例,这是来自 console.log 的副本:对象 v:0 _id:“57f07a9873023b15f016346c”生物:“jea”电子邮件:“ville”关注者:数组 [0]以下:数组[0]图像:“/uploads/57f07a9873023b15f016346c147537894901.jpg”密码:“ville”用户名:“moro”__proto:对象长度:1 proto:数组[0]
  • 我怀疑这是原型继承的问题。尝试为您的变量使用容器对象。 $scope.data.test1 然后{{data.test1}}。如果这确实解决了您的问题,我可以更详细地了解发生了什么。

标签: javascript html angularjs node.js


【解决方案1】:

您能够看到{{test1}} 而看不到其他值的原因是您有两个不同的控制器,分别称为“MainCtrl”和“NavigationCtrl”。您正在使用 ui-sref 切换状态。所以,这就是正在发生的事情。

  1. 当您单击 href 链接时,它会在 MainCtrl 中查找 search() 方法,然后将状态更改为“搜索”。

  2. 然后它将来自 NavigationCtrl 的变量和方法加载到作用域中,这就是为什么您能够看到已加载到作用域中的{{test1}}。但是您还没有调用 search() 方法,因此您无法看到其他值。

要检查我的答案,请在您的函数定义 $scope.search(); 之后在控制器内显式调用您的方法

如果您看到结果,那就是您的问题。

【讨论】:

  • 谢谢。这是有效的。我现在可以看到 test1 和 test2。出于某种原因,{{x.username}} 没有显示任何内容,但现在更容易让它工作。
【解决方案2】:

好的,此时您必须添加一个 $scope.$apply 或使用 '.then' 而不是 'success' 以使您的代码符合 promise 模式。
当您刚刚发布时,需要强制摘要循环发生。

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

【讨论】:

  • 现在它给出了一堆错误。 angular.js:13920 错误:[$rootScope:inprog]
  • Frostbch。删除你的 .success 并使用 '.then(functionSuccess, functionError);'这是 Angularjs 文档推荐使用的。