简体   繁体   English

ng-repeat后AngularJS代码不起作用

[英]AngularJS code not working after ng-repeat

I have a page where we get a list of users using angular $http from backend as shown below. 我有一个页面,我们从后端获取使用angular $ http的用户列表,如下所示。

myApp.controller("MyCntrl", function ($scope, $http) {
   $scope.users= [];
   $scope.countries = function () {
        $http.get(url).success(function (data, status, headers, config) {
            return data;
        });
    };

   $scope.getUsers = function () {
        $http.get(url).success(function (data, status, headers, config) {
            $scope.users= data;
        });
    };
});

And my html is like: 和我的HTML是这样的:

<div ng-app="myApp" ng-controller="MyCntrl" ng-init="getUsers()">
  <p ng-repeat="user in users">{{user.UserName}}</p>

  <select>
      <option ng-repeat="country in countries">{{country.CountryName}}</option>
  </select>
</div>

Everything is working fine up to displaying users. 一切正常,直到显示用户为止。

But the code after ng-repeat of users is not working. 但是,用户ng-repeat之后的代码不起作用。 I'm not able to see the list of countries in the dropdown. 我无法在下拉列表中看到国家/地区列表。 I can see the json list of both users and countries in firebug. 我可以在Firebug中看到用户和国家/地区的json列表。 Can anyone help me on this? 谁可以帮我这个事?

My countries Json is 我的国家杰森是

[{CountryId:1,CountryName:"India"}]

What i've observed is that the angular code is not working after the first ng-repeat of users. 我观察到的是,用户第一次ng-repeat后,角度代码不起作用。 When i try to see the length of countries json like 当我尝试查看json之类的国家/地区的长度时

<p>{{countries.length}}</p>

It is just printing {{countries.length}} in browser 它只是在浏览器中打印{{countries.length}}

You will have to use ng-options instead in the case of <select> : <select>的情况下,您将不得不使用ng-options

<select ng-model="selectedCountry" ng-options="country.countryName for country in countriesData">
</select>

Edit : 编辑

Replace this: 替换为:

$scope.countries = function () {
  $http.get(url).success(function (data, status, headers, config) {
    return data;
  });
};

By this: 这样:

$http.get(url).success(function (data, status, headers, config) {
  $scope.countriesData = data;
});

NOTE: Checked that url is defined ! 注意:检查url是否已定义!

You can set the returned data for $scope.countries just as you've done for $scope.users , except you don't need to define a function that you'd call in your view to get all the countries. 您可以设置为返回的数据$scope.countries就像你为做$scope.users ,除非你不需要定义你会在你看来呼吁得到所有国家的功能。

Instead, call this function in your controller which in turn will set the countries variable into the scope: 相反,请在您的控制器中调用此函数,这将把countries变量设置为作用域:

this.getCountries = function () {
    $http.get(url).success(function (data, status, headers, config) {
        $scope.countries = data;
    });
};

// call the method to get the countries 
this.getCountries();

Note that in your code you have return = data which might be causing a syntax error. 请注意,在您的代码中, return = data可能会导致语法错误。

You have call function in ng-repeat that's why its not working properly.Correct code should be like below 您在ng-repeat中有call函数,这就是它无法正常工作的原因。正确的代码应如下所示

myApp.controller("MyCntrl", function ($scope, $http) {
    $scope.users= [];
    $scope.getCountry = function () {
        $http.get(url).success(function (data, status, headers, config) {
          $scope.countries = data;
       });
    };

    $scope.getUsers = function () {
        $http.get(url).success(function (data, status, headers, config) {
           $scope.users= data;
           $scope.getCountry();
        });
    };
});

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

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