【问题标题】:Angularjs change $get source on clickAngularjs 在点击时更改 $get 源
【发布时间】:2014-01-16 17:43:32
【问题描述】:

我有一个网页使用

app.controller('listCtrl', function ($scope, $http) {
    $http.get("http://data.com/?region=north").success(function (data) {
        $scope.properties = data;
    });
});

点击按钮后,我想从不同的 URL 重新加载源代码

$http.get("http://data.com/?region=south").success(function (data) {
    $scope.properties = data;
});

有没有办法做到这一点?

【问题讨论】:

    标签: ajax json angularjs


    【解决方案1】:

    将资源的获取封装在一个参数化的函数中,这样您就可以在控制器初始化时调用它一次,然后在他们单击按钮时调用它。

    app.controller('listCtrl', function ($scope, $http) {
    
        function getResource(region) {
           $http.get("http://data.com/?region=" + region).success(function (data) {
              $scope.properties = data;
           });
        }
    
        $scope.changeRegion = getResource; // provide function for button click
    
        getResource('north'); // initialize default
    });
    

    查看:

    <button type="button" ng-click="changeRegion('south')">Change Region</button>
    

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      相关资源
      最近更新 更多