繁体   English   中英

如何从内容页面Angular JS(Ionic Framework)调用函数?

[英]How to call function from content page Angular JS(Ionic Framework)?

嗨,我是Angular,Ionic的新手。 尝试学习实践。 我的控制器文件上有一个contoller,如下所示:

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

.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.如何仅在获得功能结果后才显示内容?

任何帮助将不胜感激。

根据您的逻辑,您可以尝试这样

控制者

    .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

   })
            }

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

暂无
暂无

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

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