2017-02-19 30 views
0

嗨,我是Angular,Ionic的新手。试图与实践学习。我有我的控制器文件contoller类似的东西:如何从内容页面Angular JS(Ionic Framework)调用函数?

而且我有一个内容页面,他们都运行此控制器。内容是这样的:

.controller('GuideCtrlx', ['$scope', '$http', function($scope, $http) { 
    var city = "London"; 
    $http.jsonp("https://en.wikipedia.org/w/api.php?format=json&action=parse&page=" + city + "&prop=text&section=0&callback=JSON_CALLBACK"). 
    success(function JSON_CALLBACK(data) { 
     //stuff goes here 
    }) 
}]) 
<ion-view title="Cart" id="page2" style=""> 
    <ion-content padding="true" class="has-header"> 
     {{ }} 
    </ion-content> 
</ion-view> 

我想调用每个不同城市名称的页面上的控制器功能来获取数据。试图添加ng-init<ion-content没有工作。我有两个问题。 1.如何从上述内容页面将价值传递给控制器​​功能? 2.如何获取函数结果后才显示内容?

任何帮助将不胜感激。

+0

您的API端点不是JSONP。请查看文档https://www.mediawiki.org/api/rest_v1/ – lin

+0

重要吗?有用。 –

回答

1

按你的逻辑,你可以尝试这样的

控制器

.controller('GuideCtrlx', ['$scope', '$http', function($scope, $http) { 
     // var city = "London"; 
     $scope.getValue = function(city){ 

      $http.jsonp("https://en.wikipedia.org/w/api.php?format=json&action=parse&page="+city+"&prop=text&section=0&callback=JSON_CALLBACK"). 
      success(function JSON_CALLBACK(data) { 
       $scope.values = data; 

     //stuff goes here 

    }) 
      } 

查看1(这里的城市是伦敦)

<ion-view title="Cart" id="page2" style="" ng-init="getValue('London')"> 
    <ion-content padding="true" class="has-header"> 
    {{values}} 
    </ion-content> 
</ion-view> 

查看2(这里的城市是纽约)

<ion-view title="Cart" id="page2" style="" ng-init="getValue('New York')"> 
    <ion-content padding="true" class="has-header"> 
    {{values}} 
    </ion-content> 
</ion-view> 
相关问题