How can I add a javascript code directly into ion-view?


#1

I’ve created a <script type="text/javascript"></script> inside a ion-view but the page turns blank. I dont wanna use controller. Is that possible??


#2

why you try to add this in ion-view?
Add this in the tag in index.html and you can access the codes from any ion-view, if ionic templating done properly. Something like:

<!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>IonicApp</title>
 <link href="lib/ionic/css/ionic.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/toaster.css" rel="stylesheet">
 <script src="lib/ionic/js/ionic.bundle.min.js"></script>
 <!-- cordova script (this will be a 404 during development) -->
<script src="ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script src="lib/jquery-2.1.3.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/service.js"></script>


<script src="js/directives.js"></script>
<script>

function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}

</script>
</head>
<body ng-app="ionicApp" >
 <ion-nav-view animation="slide-left-right"></ion-nav-view>
</body>
</html>

And I can get access isNumberKey() function from every template from every ion-view.


#3

Sorry for the question but what do you mean by “ionic templating”?
How should I call isNumberKey() function from within the ion-view?


#4

hey, I mean by ionic templating if your created template is okay.

And you can call it like:

 <input  onkeypress="return isNumberKey(event)" ng-model="userpoint.point" class="button point  center-block " placeholder="00" />	

You can call the function by ng-click also even by ng-init whatever you want.
@thiago