将参数传递给离子角度服务

时间:2016-05-20 14:08:33

标签: angularjs ionic-framework

我正在尝试根据我想要在服务中评估的某些条件编写一个按钮来显示离子。我需要让服务知道谁叫它。所以我想向服务发送一个参数。但我看到无论我从模板html传递的参数是什么,该变量在服务中都不可见。示例代码如下。



   
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.service('testService', function TestService() {
  return {
    testFunc: function(param) {
      console.log("Hello"); //This prints correctly
      console.log(param); //This prints as undefined
      if (param == 1) return false;
      else
        return true;
    }
  }
})

.controller('testController', ['testService',
  function(testService) {
    console.log("Hello world");
    testService.testFunc();
  }
])

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title></title>

  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">

  <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

  <!-- ionic/angularjs js -->
  <script src="lib/ionic/js/ionic.bundle.js"></script>

  <!-- cordova script (this will be a 404 during development) -->
  <script src="cordova.js"></script>

  <!-- your app's js -->
  <script src="js/app.js"></script>
</head>

<body ng-app="starter" ng-controller="testController">

  <ion-pane>
    <ion-header-bar class="bar-stable">
      <h1 class="title">Ionic Blank Starter</h1>
    </ion-header-bar>
    <ion-content>
      <button class="button button-full button-positive icon ion-thumbsup" ng-if="testService.testFunc(10)">&nbsp;Test</button>
    </ion-content>
  </ion-pane>
</body>

</html>
&#13;
&#13;
&#13;

有没有人遇到过这种情况,有什么办法让它发挥作用?

谢谢, 作者Srini

2 个答案:

答案 0 :(得分:0)

尝试

.controller('testController', ['$scope', 'testService',
  function($scope, testService) {
    console.log("Hello world");
    $scope.testService = testService;
  }
])

答案 1 :(得分:0)

您犯了两个错误或误解了如何在您的HTML代码中使用服务。

在您的控制器中,您已使用testService.testFunc()调用服务功能。而且因为你没有在这里传递参数,所以你不会在服务中得到一些参数。

.controller('testController', ['testService', function(testService) {
    console.log("Hello world");
    testService.testFunc();   
}])

在您的html代码中,以下行调用函数testService.testFunc(10)。问题是,你的$ scope不知道这样的函数。结果将是ng-if="undefined"。您的服务永远不会被呼叫。

<button ng-if="testService.testFunc(10)">&nbsp;Test</button>

因此,有两个简单的解决方案可以解决这个问题。

您可以使用

将服务添加到$scope
.controller('testController', function($scope, testService) {
    console.log("Hello world");
    $scope.testService = testService;  
})

或者在控制器中定义一个新的$scope函数,并在此函数内调用该服务。

$scope.myFunc = function(param){
   return testService.testFunc(param);
}

<button ng-if="myFunc(10)">&nbsp;Test</button>