信息没有'点击AngularJS中的链接后不会刷新.I'我得到了{{value}},但没有得到数据

Information doesn't refresh after clicking on link in AngularJS. I've got {{ value }} but not the data

本文关键字:数据 value AngularJS 点击 链接 刷新 信息      更新时间:2023-09-26

我有一个Angular应用程序,它从一些JSON文件中获取信息。第一个JSON文件包含有关城市和管理者的数据。像这样:

[
    {
        "link" : "veliky_novgorod",
        "city" : "Великий Новгород",
        "manager" : "mr. Sminth",
    },
    {
        "link" : "magnitogorsk",
        "city" : "Магнитогорск",
        "manager" : "mr. Young",
    },...

我使用它是为了为每个城市创建一个页面。我的URL如下:...index.html#/cities/veliky_novgorod/...index.html#/cities/biisk/...index.html#/cities/biisk/mobile/等。

一切都很好,但当我尝试从一个页面转到另一个页面时,来自其他JSON(作用域)的信息不会加载。换页后我总是收到{{ value }}但当我手动刷新时,一切都会恢复正常。换句话说,单击链接后如何重新加载新页面。我试着用window.location.reload(),但它不起作用。

 <li ng-repeat="city in cities">
    <a href="#/cities/{{city.link}}" ng-click="reloadRoute()">{{city.manager}}</a>
</li>

下面是我的模块和控制器:

var curryApp = angular.module('CurryApp', [
  'ngRoute',
  'curryControllers'
]);

curryApp.config(['$routeProvider',
    function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'link_list.html',
        controller: 'CurryListCtrl'
      }).
      when('/cities/:cityId/', {
        templateUrl: 'template-total.html',
        controller: 'CurryDetailCtrl'
      }).
      when('/cities/:cityId/mobile/', {
        templateUrl: 'template-mobile.html',
        controller: 'CurryMobileCtrl'
      }).
      otherwise({
        redirectTo: '/'
      });
  }]);  

那是我的控制器:

var curryControllers = angular.module('curryControllers', []);
curryControllers.controller('CurryListCtrl', ['$scope', '$http', function($scope, $http) {
$http.get('cities.json').success(function(data) {
$scope.cities = data;
});
}]);
curryControllers.controller('CurryDetailCtrl', ['$scope', '$routeParams', '$http', '$route',
function($scope, $routeParams, $http, $route) {
    $scope.reloadRoute = function(){window.location.reload();}
    $scope.cityId = $routeParams.cityId;
    $http.get('cities.json').success(function(data) {
      $scope.cities = data;
    $http.get('paidbc.json').success(function(data2) {
      $scope.paidBc = data2;
      $scope.isActive = function(item) {
    return item.link === $scope.cityId;
};
});
    });
  }]);
curryControllers.controller('CurryMobileCtrl', ['$scope', '$routeParams',
  function($scope, $routeParams) {
    $scope.cityId = $routeParams.cityId;
  }]);

这似乎是一个与href属性有关的错误。根据文件:

错误的书写方式:

<a href="http://www.gravatar.com/avatar/{{hash}}">link1</a>

正确的书写方式:

<a ng-href="http://www.gravatar.com/avatar/{{hash}}">link1</a>

在您的情况下,使用:

<a ng-href="#/cities/{{city.link}}" ng-click="reloadRoute()">{{city.manager}}</a>

而不是仅使用CCD_ 7。