Uncaught SyntaxError: Unexpected token in Ionic - javascript

I want to make a form login. But when i want to see the result in browser, nothing happen. when i open console, the error is :
Uncaught SyntaxError: Unexpected token . services.js:3
I dont know whats wrong with the code. Anyone can help me please? I'm new in ionic
index :
<!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>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
<script src="js/constant.js"></script>
</head>
<body ng-app="starter" ng-controller="AppCtrl">
<ui-view></ui-view>
</body>
</html>
controller :
angular.module('starter')
.controller('AppCtrl', function($scope, $state, $ionicPopup, AuthService, AUTH_EVENTS){
});
services :
angular.module('starter');
.service('AuthService', function($q, $http, API_ENDPOINT) {
return {
};
})
.factory('AuthInterceptor', function($rootScope, $q, AUTH_EVENTS){
return {
};
})
.config(function ($httpProvider){
$httpProvider.interceptors.push('AuthInterceptor');
});
app.js :
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);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('outside',{
url : '/outside',
abstract : true,
templateUrl : 'templates/outside.html'
})
.state('outside.login',{
url : '/login',
templateUrl : 'templates/login.html',
controller : 'LoginCtrl'
})
$urlRouterProvider.otherwise('/outside/login')
})
form login :
<ion-view view-title="Please Sign in">
<ion-content class="padding">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Name" ng-model="user.name">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="user.password">
</label>
</div>
<button class="button button-full button-balanced" ng-click="login()">Login</button>
<button class="button button-full button-clear button-positive" ui-sref="outside.register">Register Now!</button>
</ion-content>
</ion-view>

In your services remove the ; in the first line.
angular.module('starter'); <--- This one

Related

ionic is not detecting a controller

I created a blank project for ionic v1 and added a controller inside app.js file:
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if (window.cordova && window.Keyboard) {
window.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider){
$stateProvider
.state('app', {
url: '/app',
templateUrl: 'main.html',
controller: 'AppCtrl',
})
})
.controller('AppCtrl',function($scope){
console.log("test");
})
Also I added main.html file, which is just a blank file with "TEST" text
After executing ionic serve command and navigating to http://localhost:8100/#/app console command is not executed, and text isn't shown. I receive no error messages.
UPDATE: index.html file
<!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 rel="manifest" href="manifest.json">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content name="content">
</ion-content>
</ion-pane>
</body>
</html>
UPDATE: Found a solution
index.html was missing ion-nav-view tags
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
Added it and everything started to work.
Found a solution
index.html was missing ion-nav-view tags
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
Added it and everything started to work.

Tried to load angular more than once on ionic

i make a crud with route in ionic. but the routing doesnt work. when i run this project, it doesnt show anything like . when i look the console, there is a warning :
WARNING: Tried to load angular more than once.
can anyone help me please?
index :
<!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 rel="manifest" href="manifest.json">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.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 -->
</head>
<body ng-app="myAPP">
<ion-pane>
<ion-header-bar class="bar-stable">
<div class="bar bar-header bar-calm">
<h1 class="title" align="center">CRUD</h1>
</div>
<div class="tabs-striped tabs-top">
<div class="tabs">
<a class="tab-item" href="#/">List</a>
<a class="tab-item" href="#/addData">Add Data</a>
<a class="tab-item" href="#/editData">Edit Data</a>
</div>
</ion-header-bar>
<ion-content>
<div>
<div ng-view></div>
</div>
</ion-content>
</ion-pane>
<script src="js/angular.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/route.js"></script>
<script src="controller/controller.js"></script>
<script src="controller/edit.js"></script>
</body>
</html>
app. js :
var app = angular.module('myAPP', ['ionic','ngRoute'])
route.js :
app.config(['$routeProvider',function($routeProvider){
$routeProvider.
when('/',{
templateUrl : '../pages/list.html',
controller : 'controller'
})
.when('/addData',{
templateUrl : '../pages/addData.html',
controller : 'controller'
})
.when('/editData/:id',{
templateUrl : '../pages/update.html',
controller : 'controllerEdit'
})
.otherwise({
redirectTo : '/'
});
}])
list.html :
<div class="container">
<h2>{{title}}</h2>
</br>
</br>
<table class="table table-striped" ng-init="getData()">
<tr>
<th>NO</th>
<th>Nama</th>
<th>Alamat</th>
<th>Action</th>
</tr>
<tr ng-repeat="x in dataList track by $index">
<td>{{$index+1}}</td>
<td>{{x.nama}}</td>
<td>{{x.alamat}}</td>
<td>
<button type="button" class="btn btn-info" ng-click="edit(x.id)">Edit</button>
<button type="button" class="btn btn-danger" ng-click="delete(x.id)">Delete</button>
</td>
</tr>
</table>
</div>
ionic.bundle.js is a concatenation of:
ionic.js,
angular.js,
angular-animate.js
angular-sanitize.js,
angular-ui-router.js
ionic-angular.js
and your app.js should be
var app = angular.module('ionicApp', [ 'ionic' ])
app.config(function($stateProvider, $urlRouterProvider) {
// Ionic uses AngularUI Router which uses the concept of states
// Learn more here: https://github.com/angular-ui/ui-router
// Set up the various states which the app can be in.
// Each state's controller can be found in controllers.js
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url : "/tab",
abstract : true,
templateUrl : "templates/tabs.html",
controller : 'dashboardCtrl'
})
// Each tab has its own nav history stack:
.state('tab.overview', {
url : '/overview',
views : {
'tab-overview' : {
templateUrl : 'templates/tab-overview.html',
//controller : 'overviewCtrl'
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/overview');
})

Cordova ng-route not working properly

I have the below angular application.
// app.js
var rippleApp = angular.module('rippleApp', ['ngRoute', 'ngAnimate', 'ngAria', 'ngMaterial']);
// configure our routes
rippleApp.config(function ($routeProvider, $compileProvider) {
$routeProvider
// route for the home page
.when('/home', {
templateUrl: '../pages/home.html',
controller: 'mainController'
})
// route for the about page
.when('/about', {
templateUrl: '../pages/about.html',
controller: 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl: '../pages/contact.html',
controller: 'contactController'
});
});
// create the controller and inject Angular's $scope
rippleApp.controller('sideNavController', function ($scope, $mdSidenav) {
$scope.openLeftMenu = function () {
$mdSidenav('left').toggle();
};
$scope.openRightMenu = function () {
$mdSidenav('right').toggle();
};
});
rippleApp.controller('mainController', function ($scope, $mdSidenav) {
// create a message to display in our view
$scope.pageClass = 'page-home';
$scope.message = 'Everyone come and see how good I look!';
$scope.openLeftMenu = function () {
$mdSidenav('left').toggle();
};
$scope.openRightMenu = function () {
$mdSidenav('right').toggle();
};
});
rippleApp.controller('notificationsController', function ($scope, $mdToast, $document) {
$scope.showToast1 = function () {
$mdToast.show(
$mdToast.simple()
.textContent('Hello World!')
.hideDelay(3000)
);
};
$scope.showToast2 = function () {
var toast = $mdToast.simple()
.textContent('Hello World!')
.action('OK')
.highlightAction(false);
$mdToast.show(toast).then(function (response) {
if (response == 'ok') {
alert('You clicked \'OK\'.');
}
});
}
});
rippleApp.controller('aboutController', function ($scope) {
$scope.pageClass = 'page-about';
$scope.message = 'Look! I am an about page.';
});
rippleApp.controller('contactController', function ($scope) {
$scope.pageClass = 'page-contact';
$scope.message = 'Contact us! JK. This is just a demo.';
});
and the below index file
<!DOCTYPE html>
<html>
<head>
<!--
Customize the content security policy in the meta tag below as needed. Add 'unsafe-inline' to default-src to enable inline JavaScript.
For details, see http://go.microsoft.com/fwlink/?LinkID=617521
-->
<!--
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
-->
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<!--Scripts-->
<!--Stylesheets-->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
<link rel="stylesheet" href="css/bundle.css" />
<link rel="stylesheet" href="css/index.css">
<title>RippleAlpha</title>
</head>
<body ng-app="rippleApp" ng-controller="mainController">
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<div id="sideNavContainer" layout="row" ng-cloak>
<md-sidenav md-component-id="left" class="md-sidenav-left">Test case</md-sidenav>
<md-content>
<md-button class="navbar-brand" ng-click="openLeftMenu()">SM</md-button>
</md-content>
</div>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a ng-href="#home"><i class="fa fa-home"></i> Home</a></li>
<li><a ng-href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a ng-href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
</div>
</nav>
<div id="toastContainer" ng-controller="notificationsController as ctrl" layout="row" ng-cloak>
<md-button ng-click="showToast1()">notification</md-button>
<md-button ng-click="showToast2()">notification - callback</md-button>
</div>
</header>
<div id="main">
<div class="page {{ pageClass }}" ng-view></div>
</div>
<script src="scripts/bundle.js"></script>
<script src="scripts/app.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>
</body>
</html>
I'm able to get routes and such working in browser / ripple emulator but when debugging on device, I am unable to use the routing and navigating does nothing.
This is angular 1.5.X
I figured out my problem.
For further reference from anyone looking for a solution to this issue.
My problame was that I was suing the url '../pages*'
Since this is not an http url and rather a fail:/// i just had to change it to start at 'pages/' a relative url.
Problem solved and working now.

creating independent controllers in angular with ionic

I am new to angular and trying to create new login app, i want to maintain separate controller but when separate the controllers from controllers.js I am getting function got undefined error.
here is My code:
app.js
<!-- begin snippet: js hide: false -->
// MainCtrl.js
angular.module('myapp.controllers', [])
.controller('MainCtrl',
function($scope, $state) {
console.info("inside main ctrl");
});
//
//index.html
<!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>
<script src="js/services.js"></script>
<script src="js/controllers/LoginCtrl.js"></script>
<script src="js/controllers/MainCtrl.js"></script>
<!-- <script src="js/controllers.js"></script> -->
</head>
<body ng-app="starter" ng-controller = "MainCtrl">
<!--
The nav bar that will be updated as we navigate between views.
-->
<ion-nav-bar class="bar-stable">
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<!--
The views will be rendered in the <ion-nav-view> directive below
Templates are in the /templates folder (but you could also
have templates inline in this html file if you'd like).
-->
<ion-nav-view></ion-nav-view>
</body>
</html>
login.html
<ion-view view-title="Login" name="login-view">
<ion-content class="padding">
<div class="list list-inset">
<label class="item item-input">
<input type="text" placeholder="Username" ng-model="data.username">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="data.password">
</label>
</div>
<button class="button button-block button-calm" ng-click="login()">Login</button>
</ion-content>
</ion-view>
got this error:
ionic.bundle.js:26771 Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined
http://errors.angularjs.org/1.5.3/ng/areq?p0=LoginCtrl&p1=not%20a%20function%2C%20got%20undefined
at ionic.bundle.js:13415
at assertArg (ionic.bundle.js:15191)
at assertArgFn (ionic.bundle.js:15201)
at $controller (ionic.bundle.js:23350)
at self.appendViewElement (ionic.bundle.js:59763)
at Object.switcher.render (ionic.bundle.js:57874)
at Object.switcher.init (ionic.bundle.js:57794)
at self.render (ionic.bundle.js:59623)
at self.register (ionic.bundle.js:59581)
at updateView (ionic.bundle.js:65261)
The code works as expected if I replace the myapp.controller with starter.controller in LoginCtrl.js and adding starter.controller along with myapp.controller in app.js.
angular.module('starter', ['ionic', 'myapp.controllers', 'starter.controllers'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
cache:false,
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl'
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
// LoginCtrl.js
angular.module('starter.controllers',[])
.controller('LoginCtrl', function($scope, $state){
$scope.login = function() {
console.log("LOGIN user");
// $state.go('app.home')
}
});

Argument 'LoginCtrl' is not a function, got undefined in the Ionic

I'm trying to create simple navigation in my ionic application, but I get following error.
Argument 'LoginCtrl' is not a function, got undefined in the Ionic
What I did wrong?
index.html:
<!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>
<script src="lib/angular-route.js"></script>
<script src="lib/angular-cookies.js"></script>
<script src="lib/ng-cordova/dist/ng-cordova.min.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>
<script src="js/restService/login.js"></script>
</head>
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
</html>
app.js:
angular.module('starter', ['ionic',
'Authentication',
'ngCookies'])
.config(function($stateProvider, $urlRouterProvider) {
//$urlRouterProvider.otherwise('views/main')
$stateProvider.state('login', {
url: '/login',
controller: 'LoginCtrl',
templateUrl: 'views/throbber.html'
})
})
.run(['$ionicPlatform', '$rootScope', '$location', '$cookieStore', '$http',
function($ionicPlatform, $rootScope, $location, $cookieStore, $http) {
$ionicPlatform.ready(function() {
try {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
} catch (ex) {
alert(ex);
}
});
}]);
angular.module('Authentication', []);
js/restService/login.js:
angular.module('Authentication')
.controller('LoginCtrl' [
function () {
alert('test');
}
]);
views/throbber.html:
<div>THROBBER</div>
your login.js is loaded after app.js.
When you are configuring routes, LoginCtrl controller is not available to angular, therefore it is "undefined"/"not a function".
You need to have the contents of login.js moved into app.js, or have your gulp/grunt task runner to combine the contents.

Categories

Resources