I was just trying to make the angular bootstrapping work where I want to get my app loaded only after retrieving some data from the server using the service call. However I am not able to make this work with the $routeprovider. My routes doesn't work after this bootstrapping. can anyone know what is wrong I am doing here???
var myApp = angular.module('myApp', [ 'ngRoute', 'ui.bootstrap',
'dataHandler', 'underscore','ngAnimate', 'angular-loading-bar' ]);
fetchData();
function fetchData() {
var initInjector = angular.injector(["ng"]);
var $http = initInjector.get("$http");
return $http.get("/path/to/data.json").then(function(response) {
bootstrapApplication();
}, function(errorResponse) {
// Handle error case
bootstrapApplication();
});
}
function bootstrapApplication() {
angular.element(document).ready(function() {
myApp.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
redirectTo : '/dashboard'
}).when('/dashboard', {
menuItem: 'DASHBOARD',
templateUrl : '../views/customer/dashboard/customerDashboard.html',
controller : 'CustomerDashboardControllerPT',
controllerAs : 'custdashboard'
}).otherwise({
redirectTo : '/'
});
//$locationProvider.html5Mode(true);
}]);
angular.bootstrap(document, ["myApp"]);
});
}
It seems it doesn't recognize my MainController also. In my html file I have code like:
<div class="container" ng-controller="MainController as main">
<div class="row">
<div class="col-sm-12"></div>
</div>
</div>
<script type="text/javascript"
src="../frameworks/jquery/jquery-1.11.2.min.js"></script>
<script type="text/javascript"
src="../frameworks/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="../frameworks/angular/angular.min.js"></script>
<script type="text/javascript"
src="../frameworks/angular/angular-route.js"></script>
<script type="text/javascript" src="../frameworks/angular/angular-animate.js"></script>
<script type="text/javascript" src="../frameworks/angular/ui-bootstrap.js"></script>
<script type="text/javascript" src="../frameworks/underscore/underscore.js"></script>
<script type="text/javascript" src="../frameworks/moment/moment.min.js"></script>
<script type="text/javascript" src="../frameworks/angular/loading-bar.js"></script>
<script type="text/javascript" src="../js/controllers/MainController.js"></script>
Related
I want to translate my website/webpage in 2 languages but when I press the buttons to change the language nothing happens and this error appeared in console.
Index.html
<html ng-app='mpdmApp' lang="en">
<head>
<base href="/">
<meta charset="utf-8">
<script src="assets/js/libs/angular.min.js"></script>
<script src="assets/js/libs/angular-animate.js"></script>
<script src="assets/js/libs/angular-sanitize.js"></script>
<script src="assets/js/libs/angular-messages.min.js"></script>
<script src="assets/js/libs/angular-aria.js"></script>
<script src="assets/js/libs/angular-route.js"></script>
<script src="assets/js/libs/angular-material.min.js"></script>
<script src="app/app.js"></script>
<script src="app/app.config.js"></script>
<script src="app/seafood/seafood.module.js"></script>
<script src="app/seafood/seafood.component.js"></script>
<!--scripturi angular translate-->
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate/2.18.1/angular-translate.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-interpolation-messageformat/2.18.1/angular-translate-interpolation-messageformat.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-storage-cookie/2.18.1/angular-translate-storage-cookie.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-storage-local/2.18.1/angular-translate-storage-local.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-loader-url/2.18.1/angular-translate-loader-url.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-loader-static-files/2.18.1/angular-translate-loader-static-files.js"></script>
<script src="https://cdn.rawgit.com/angular-translate/bower-angular-translate-handler-log/2.18.1/angular-translate-handler-log.js"></script>
<script src="app/script.js"></script>
</head>
<body class="mp_sc_0">
<mpdm-menu></mpdm-menu>
<div ng-controller="MainCtrl">
<div ng-view class="mpdmView"></div>
</div>
</body>
app.js
var translationsEN = {
seafood: 'Seafood',
grill: 'Grill',
BUTTON_LANG_DE: 'German',
BUTTON_LANG_EN: 'English'
};
var translationsDE= {
seafood: 'Preparate peste',
grill: 'Preparate la gratar',
BUTTON_LANG_DE: 'Deutsch',
BUTTON_LANG_EN: 'Englisch'
};
var mpdmApp = angular.module('mpdmApp', [
'ngRoute', 'ngAnimate', 'ngSanitize','ngMaterial', 'ngMessages', 'mpdmSeafood', 'mpdmGrill', 'mpdmMeniu', 'pascalprecht.translate'
]);
mpdmApp.config(['$translateProvider', function ($translateProvider) {
// add translation tables
$translateProvider.translations('en', translationsEN);
$translateProvider.translations('de', translationsDE);
$translateProvider.fallbackLanguage('de');
$translateProvider.preferredLanguage('en');
}]);
//----controllerul pentru traduceri
mpdmApp.controller('MainCtrl', ['$translate', '$scope', function ($translate, $scope) {
$scope.changeLanguage = function (langKey) {
$translate.use(langKey);
};
}]);
mainpage.html - In this page I put the words that I want to be translated.
I believe that because of that error in console I can't switch the languages.
<div class="btn1">
</div>
<div class="btn2>
{{'grill' | translate}}
</div>
<button ng-click="changeLanguage('de')" translate="BUTTON_LANG_DE"></button>
<button ng-click="changeLanguage('en')" translate="BUTTON_LANG_EN"></button>
app.config.js
mpdmApp.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
template: '<mpdm-main-page></mpdm-main-page>'
})
.when('/seafood', {
template: '<mpdm-seafood></mpdm-seafood>'
})
.when('/contact', {
template: '<mpdm-contact></mpdm-contact>'
})
.when('/desprenoi', {
template: '<mpdm-desprenoi></mpdm-desprenoi>'
})
.when('/multumim', {
template: '<mpdm-multumim></mpdm-multumim>'
})
.when('/grill', {
template: '<mpdm-grill></mpdm-grill>'
})
.when('/meniu', {
template: '<mpdm-meniu></mpdm-meniu>'
}).
when('/notfound', {
template: '<mpdm-coming-soon></mpdm-coming-soon>'
}).
otherwise({redirectTo:'/notfound'});
$locationProvider.html5Mode(true);
});
I am trying to use routing in Cordova but is not working. All js files are in www file.
Controllers are working. When I try in url to write:
http://localhost:8000/info
it says error
Cannot GET /info
I think I write in code everything correctly.
In html inside head I have put the:
<base href="/"/>
also the name module project and in body:
<ng-view></ng-view>
<script type="text/javascript" src="/js/angular.min.js"></script>
<script type="text/javascript" src="/js/angular-route.min.js"></script>
<script type="text/javascript" src="/js/angular-animate.min.js"></script>
<script type="text/javascript" src="/js/angular-aria.js"></script>
<script type="text/javascript" src="/js/angular-messages.min.js"></script>
<script type="text/javascript" src="/js/angular-material.min.js"></script>
<script type="text/javascript" src="/js/fontawesome5/fontawesome-all.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
<script src="/js/controllers/MainController.js"></script>
<script src="/js/controllers/InfoController.js"></script>
In my app.js file
var app = angular.module('project', ['ngRoute', 'ngMaterial', 'ngMessages']);
in routes.js (the below alert it didn't works):
app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider){
alert('routes working!');
$routeProvider
.when("/", {
templateUrl: "views/login.html",
controller: "MainController"
})
.when("/info", {
templateUrl: "views/info.html",
controller: "InfoController"
})
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
}]);
in InfoController:
app.controller("InfoController", ["$scope","$http",function($scope,$http){
$scope.info = "info page here!";
}]);
What am I missing and it isn't working? What to try for debug?
Any suggestions?
I was forgotten to put in html the routes.js file:
<script type="text/javascript" src="/js/routes.js"></script>
Now everything works!
My code is as shown below:
index.html
<!DOCTYPE html>
<html>
<head>
<title>quflip</title>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/login.css">
</head>
<body ng-app="quflipMobWeb">
<ng-view></ng-view>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="js/services.js"></script>
<script src="js/controller/loginController.js"></script>
<script src="js/controller/homeController.js"></script>
<script src="js/app.js"></script>
<script src="js/controller/driverController.js"></script>
<script src="js/controller/driversController.js"></script>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
</script>
</body>
</html>
home.html
<div>
<div id="mySidenav" class="sidenav">
×
About
Services
Clients
Contact
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
homeController.js
angular.module('quflipMobWeb.homeController', []).
controller('homeController', function($scope) {
// console.log("home called");
});
app.js
angular.module('quflipMobWeb', [
'quflipMobWeb.services',
'quflipMobWeb.controllers',
'quflipMobWeb.login',
'quflipMobWeb.homeController',
'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when("/home", { templateUrl: "template/home.html", controller: "homeController" }).
when("/login", { templateUrl: "template/login.html", controller: "loginController" }).
otherwise({ redirectTo: '/login' });
}]);
For detailed code, I have given my repository url here:
https://github.com/mrugesh008/testRepo.git
This is the website which I am following for achieving the effect
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sidenav
Your code almost works well. you can just add ng-style:
app.controller('homeController', function($scope) {
console.log("home called");
$scope.myStyle = {
width: '0px'
};
$scope.openNav = function() {
$scope.myStyle.width = '250px';
}
$scope.closeNav = function() {
$scope.myStyle.width = '0px';
}
});
Home.html
<div>
<div id="mySidenav" class="sidenav" ng-style="myStyle">
×
About
Services
Clients
Contact
</div>
<span style="font-size:30px;cursor:pointer" ng-click="openNav()">☰ open</span>
</div>
Demo Plunker
I am using intellij and it created a template for me. I am trying to use:
app.config(function($routeProvider, $locationProvider) {
to direct my pages. However currently the default index page in rendering. This is just my attempt to figure out how things are connecting.
configuration.js
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/',{
templateUrl: "app/pages/mainPage/mainPage.html",
controller:"mainPageController"
});
$routeProvider.otherwise({redirectTo: '/view1'});
});
mainPage.html
<div>
<h2>Hello World</h2>
</div>
index.html
<body>
<ul class="menu">
<li>view1</li>
<li>view2</li>
</ul>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please upgrade your browser to improve your experience.</p>
<![endif]-->
<div ng-view></div>
<div>Angular seed app: v<span app-version></span></div>
<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
-->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="app.js"></script>
<script src="view1/view1.js"></script>
<script src="view2/view2.js"></script>
<script src="components/version/version.js"></script>
<script src="components/version/version-directive.js"></script>
<script src="components/version/interpolate-filter.js"></script>
<script src="pages/mainPage/mainPageController.js"></script>
<script src="configuration.js"></script>
</body>
</html>
app.js
// Declare app level module which depends on views, and components
var app = angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.version'
]);
view1.js
'use strict';
angular.module('myApp.view1', ['ngRoute'])
//.config(['$routeProvider', function($routeProvider) {
// $routeProvider.when('/view1', {
// templateUrl: 'view1/view1.html',
// controller: 'View1Ctrl'
// });
//}])
.controller('View1Ctrl', [function() {
}]);
view2.js
'use strict';
angular.module('myApp.view2', ['ngRoute'])
//.config(['$routeProvider', function($routeProvider) {
// $routeProvider.when('/view2', {
// templateUrl: 'view2/view2.html',
// controller: 'View2Ctrl'
// });
//}])
.controller('View2Ctrl', [function() {
}]);
I am trying to render mainPage.html and handle all the redirects in one file i.e configuration.js
I'm new to AngularJS and I'm trying to declare a second controller to a new view in my page. I have a logincontroller and when I succeed the login I get to the Home.html.
When opening Home.html I get the following error below..It looks like it can't recognize my ModelController/ModalService..What am I missing here?
Error: [$injector:unpr] http://errors.angularjs.org/1.5.0-beta.1/$injector/unpr?p0=ModalServiceProvider%20%3C-%20ModalService%20%3C-%20ModalController
Index.html:
<!DOCTYPE html>
<html ng-app="PVM">
<head>
<title>My Ang</title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.min.css" rel="stylesheet" />
<link href="Content/MyCss.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
<div>
<div ng-view></div>
</div>
<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-route.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-cookies.min.js"></script>
<script src="Scripts/Services/ModalService.js"></script>
<script src="Scripts/Services/AuthenticationService.js"></script>
<script src="Scripts/Controller/LoginController.js"></script>
<script src="Scripts/Controller/ModalController.js"></script>
<script src="Scripts/MyApp.js"></script>
</body>
</html>
Home.html:
<div class="container" ng-controller="ModalController">
<div class="col-xs-2">
<button class="btn btn-group-justified" ng-init="testfunc()">press me</button>
</div>
</div>
MyApp.js:
angular.module('MyApp', [
'Authentication',
'Modal',
'ngRoute',
'ngCookies'
])
.config([
'$routeProvider', function($routeProvider) {
$routeProvider
.when('/Login', {
controller: 'LoginController',
templateUrl: 'Login.html'
})
.when('/Home', {
controller: 'ModalController',
templateUrl: 'Home.html'
})
.otherwise({
redirectTo: '/Login'
});
}
]);
ModalController.js:
angular.module('Modal')
.controller('ModalController',
['$scope', 'ModalService',
function ($scope, ModalService) {
$scope.testfunc = function() {
alert('weeee');
}
}]);
ModalService.js:
angular.module('Modal', []);
angular.module('Modal')
.factory('ModalService'
['$http',
function ($http) {
var service = {};
return service;
}
]);
You need to include app.js after including services and controllers. And controllers after services.
it should be like this:
<script src="Scripts/Services/ModalService.js"></script> <!--ModalService is being defined here -->
<script src="Scripts/Services/AuthenticationService.js"></script>
<script src="Scripts/Controller/LoginController.js"></script>
<script src="Scripts/Controller/ModalController.js"></script> <!-- ModalService is being injected here -->
<script src="Scripts/MyApp.js"></script> <!-- All of the things are set in place here -->
In fact, the best practice is to include all of them in the bottom of your <body>.
I found the answer..The problem was that I was missing a comma :(
ModalService.js should look like this:
angular.module('Modal', []);
angular.module('Modal')
.factory('ModalService', <------ COMMA MISSING :(
['$http',
function ($http) {
var service = {};
return service;
}
]);