我正在制作一个与离子应用程序,我有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) {
});发布于 2015-03-24 01:21:36
您将脚本放在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元素来完成所有操作。
https://stackoverflow.com/questions/29200475
复制相似问题