你在哪里把特定于页面的脚本放在Ionic中?

时间:2015-03-22 22:13:22

标签: javascript jquery angularjs ionic

我正在创建一个带有离子的应用程序,每个页面都有JS脚本。例如,一个页面有一个脚本,它将根据一天中的时间更改消息,而另一个页面有一个jQuery .load()脚本。似乎都没有奏效。它们是DOM操纵的,位于页面的底部。

<ion-view view-title="Welcome" style="text-align:center; padding:20px;">
  <ion-content>
    <h1 id="time">Hello!</h1>
      <p>Welcome to the app. We are thrilled to share our new app with you!</p>
  </ion-content>
   <script>
       date = new Date();
       var hours = date.getHours();
       console.log(hours);
       if (hours >= 5 && hours <= 11) //MESSAGE FOR MORNING
document.getElementById('time').innerHTML = 'Good Morning.';
else if (hours >= 12 && hours <= 17) //MESSAGE FOR AFTERNOON
document.getElementById('time').innerHTML = 'Good Afternoon.';
else if (hours >= 18 && hours <= 20) //MESSAGE FOR EVENING (6pm-8pm)
document.getElementById('time').innerHTML = 'Good Evening.';
else //MESSAGE FOR NIGHT (9pm-4am)
document.getElementById('time').innerHTML = 'Good Night.';</script> 
</ion-view>

Controllers.js:

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

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);

    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    $timeout(function() {
      $scope.closeLogin();
    }, 1000);
  };
})

.controller('PlaylistsCtrl', function($scope) {
  $scope.playlists = [
    { title: 'Reggae', id: 1 },
    { title: 'Chill', id: 2 },
    { title: 'Dubstep', id: 3 },
    { title: 'Indie', id: 4 },
    { title: 'Rap', id: 5 },
    { title: 'Cowbell', id: 6 }
  ];
})

.controller('PlaylistCtrl', function($scope, $stateParams) {
});

1 个答案:

答案 0 :(得分:0)

您将脚本放在controller.js文件中。您添加以下代码:

.controller('PageCtrl', function($scope){
//Insert function code here
})

在离子页面的<ion-view>参数中,您将"ng-controller"设置为PageCtrl。在DOM操作div中,你可以输入<div>{{hello}}</div>之类的内容。然后通过在控制器内部使用$scope.hello = "Your String";来操作DOM元素来完成所有操作。