【问题标题】:how to call a controller in ionic?如何在离子中调用控制器?
【发布时间】:2015-04-27 11:31:22
【问题描述】:

在我的 ionic 空白应用程序中,我有两个 html 文件 index.html 和 category.html。 我在 app.js 中为 index.html 编写控制器,例如

.controller('AppCtrl',function($scope){
  $scope.menu = function() {
    console.log('yesytest');
    window.location = "category.html";
  };

})

这工作正常,到达类别页面后,我在 app.js 中创建另一个控制器

    controller('categoryCtrl',function($scope){
        console.log('ffffffffff');
    $scope.beauty = function() {
        console.log('yesytest');
        window.location = "categorydetails.html";

    };

});

我在 category.html 内的按钮上添加了一个 ng-click 功能,但是当我点击该按钮时,它没有调用控制器?

【问题讨论】:

    标签: angularjs cordova ionic-framework ionic


    【解决方案1】:

    您可以在以下位置定义控制器: - 在你的 ui 路由器状态中定义它

        .state('camera',{
                url:"/camera",
                cache: false,
                controller:"CameraCtrl",
                templateUrl:'app/views/loading/index.html'
        })
    

    或将其定义为游览模板 category.html

    <div ng-controller="categoryCtrl"> .... </div>
    

    这是标准的 AngularJS 功能,对 ionic 没什么特别的

    【讨论】:

      【解决方案2】:

      如果你使用这个:

      .state('category',{
      url:"/category",
      controller:"categoryCtrl",
      templateUrl:'templates/category.html'
      })
      

      您可以使用 ui-sref="stateId" 进行重定向 示例:

        <a ui-sref="category"></a>
      

      【讨论】:

        【解决方案3】:

        ..嗨,请路由您的控制器,只需定义它们..这可能会有所帮助.. --> http://learn.ionicframework.com/formulas/navigation-and-routing-part-1/ ^_^

        //app.js

         .state('category',{
            url:"/category",
            controller:"categoryCtrl",
            templateUrl:'templates/category.html'
         })
        

        //index.html

         <a ng-href="#/category"></a>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-26
          • 1970-01-01
          • 2017-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多