I have a folder structure like this
dashboard (folder)
-- index.html
route.js
index.html
And I my index.html I wrap my ui-view
<head>
<style type="text/css">
h1{color:red}
</style>
</head>
<body>
<ui-view></ui-view>
</body>
Then in route.js I have
var app = angular.module('cartApp', ['ui.router']);
app.config(['$stateProvider','$urlRouterProvider','$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard/index.html'
})
}]);
But when I go to localhost:3000/dashboard I'm seeing a h1 without color. It's unable to load its parent's 'stuff'. Any idea why?
Related
I have a gulp server with AngularJS 1.6 application.
I have an ng-router. I am trying to remove #! symbol from the url, so I have used ng-router. I have used $locationProvider.html5Mode(true); and in index.html I have used <base href="/"> at the top.
but when i refresh the page it is showing Cannot GET /ui/index.html/
I am using gulp server.
app.js
var app = angular.module("apollo", ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/ui/index.html/', {
templateUrl: 'root.html',
controller: 'HeaderCtrl'
})
.when('/ui/:appName/', {
templateUrl: 'apps/' + localStorage.getItem('locationApp') + "/controllers/home/templates/home.template.html"
})
.otherwise({
redirectTo: '/ui/index.html/'
});
$locationProvider.html5Mode(true);
});
index.html
<!DOCTYPE html>
<html>
<head>
<title>Apollo</title>
<base href="/">
<script src="library/angular.min.js"></script>
<script src="library/angular-route.js"></script>
<script src="app.js"></script>
<script src="services/apollo-headerCtrl.js"></script>
<script src="services/AppCtrl.js"></script>
</head>
<body ng-app="apollo">
<div ng-view></div>
</body>
</html>
angularjs route not working properly . first page show other page not switch the view . here is the code
script.js
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "home.html",
})
.when("/About", {
templateUrl: "About.html",
})
.when("/contact", {
templateUrl: "contact.html",
});
});
index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome Home</title>
</head>
<body ng-app="myApp">
<p>Main</p>
About
contact
<h3> welcome to the page</h3>
<div ng-view></div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script src="script.js"></script>
</body>
</html>
home.html
<h1>Welcome Home </h1>
About.html
<h1>Welcome About </h1>
contact.html
<h1>Welcome Contact </h1>
Also be careful about dependency injection.
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "home.html",
})
.when("/About", {
templateUrl: "About.html",
})
.when("/contact", {
templateUrl: "contact.html",
});
$locationProvider.html5Mode(true);
}]);
I have a root index.html file which has a ui-view tag, to which a main template file home.html is injected.
home.html has a ui-view tag which needs to change depending on the current route. I followed the official ui-router documentation and wrote the app.js, but the nested ui-view in home.html tag is not being injected with a view.
The structure is as follows:
index.html
| home.html
- dashboard.html
- tables.html
index.html has a ui-view tag which shows home.html. home.html is a template with a ui-view tag which needs to either show dashboard.html or tables.html depending on the route.
Source:
index.html -
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="lib/css/main.min.css"/>
<script src="lib/js/main.min.js"></script>
<script type="text/javascript" src="js/dashboard.min.js"></script>
</head>
<body ng-controller="MasterCtrl">
<div ui-view></div>
</body>
</html>
templates/home.html -
<div id="page-wrapper" ng-class="{'open': toggle}" ng-cloak>
<!-- Sidebar -->
<div ng-include="sidebar.html" scope="" onload=""></div>
<!-- End Sidebar -->
<div id="content-wrapper">
<div class="page-content">
<!-- Header Bar -->
<div ng-include="header.html" scope="" onload=""></div>
<!-- End Header Bar -->
<!-- Main Content -->
<div ui-view></div>
</div><!-- End Page Content -->
</div><!-- End Content Wrapper -->
module.js
angular.module('app', ['ui.bootstrap', 'ui.router', 'ngCookies']);
routes.js
'use strict';
angular.module('app').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For unmatched routes
$urlRouterProvider.otherwise('/');
// Application routes
$stateProvider
.state('home', {
abstract: true,
url: '/',
templateUrl: 'templates/home.html'
})
.state('home.dashboard', {
url: '/dashboard',
templateUrl: 'templates/dashboard.html'
})
.state('home.tables', {
url: '/tables',
templateUrl: 'templates/tables.html'
});
}
]);
On add them as routes without inheriting from home, they're being injected normally into index.html, which means it's not an issue with the route themselves.
How can I inherit the template and inject the views, depending on the route?
You need to add ui-router as a dependency to the module
angular.module('app',['ui.router'])
also make sure you have added references
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<!-- UI-Router -->
<script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>
DEMO
Solved my issue. I removed the url tag from abstract state home, and just set home.dashboard to /.
This works:
'use strict';
angular.module('app').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For unmatched routes
$urlRouterProvider.otherwise('/');
// Application routes
$stateProvider
.state('home', {
abstract: true,
templateUrl: 'templates/home.html'
})
.state('home.dashboard', {
url: '/',
templateUrl: 'templates/dashboard.html'
})
.state('home.tables', {
url: '/tables',
templateUrl: 'templates/tables.html'
});
}
]);
I'm New to Angular, and learning through the tutorials on Thinkster. I'm at the Angular Routing -> Adding a New State step.
I am trying to use ui-router to render an inline template, but the template is not appearing. Here is my index.html (abridged) :
<html>
<head>
<!-- My js files are loaded here -->
</head>
<body ng-app='flapperNews'>
<div class ='row'>
<div class="col-md-6 col-md-offset-3">
<div ui-view></div>
</div>
</div>
<script type="text/ng-template" id="home.html">
<!-- My template written as plain html>
</script>
</body>
</html>
And this is how my routing is currently handled in app.js :
angular.module('flapperNews',['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider){
return;
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}]);
It is my understanding that ui-router should parse the url and render the appropriate template within the div ui-view tags. (Thinkster says the tag should be 'ui-view' but the ui-router docs seemed to indicate otherwise; I have tried both ).
My page renders blank, and there are no errors logged to the console. For what its worth, I am doing the tutorial with just local files, and since I've added the routing code my url has a # appended to it i.e file://blah/blah/FlapperNews/index.html# and I'm unsure why.
Just tries the following code and seem to work. It is based on the link you provided.
What I fixed from your code:
id="home.html" to id="/home.html"
comment is not closed: <!-- My template written as plain html>
delete return; from config function
removed controller since there wasn't any code for it
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body ng-app='flapperNews'>
<div class ='row'>
<div class="col-md-6 col-md-offset-3">
<div ui-view></div>
</div>
</div>
<script type="text/ng-template" id="/home.html">
test content
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
angular.module('flapperNews', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html'
// controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}])
</script>
</body>
</html>
Delete the return in your configuration function.
angular.module('flapperNews',['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}]);
I try to run angular-ui-router to handle my views but i have a problem.
The two links of the following view are not clickable.
Angular change variable with link label but i can't click on.
I have this view :
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>App</h1>
<nav>
<a ui-shref="app">{{link.home}}</a>
<a ui-shref="app.front.signin">{{link.signin}}</a>
</nav>
<div ui-view="content">
</div>
</body>
</html>
I use this code. It do not returns errors
. All modules (localStorage ... are included) but links are not clickable.
/**
* Declaration of MyAppControllers module
*/
MyAppControllers = angular.module('MyAppControllers',[]);
/**
* Declaration of MyApp Application
*/
MyApp = angular.module('MyApp', ['MyAppControllers','LocalStorageModule','ui.router']);
MyApp.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
//
// Now set up the states
$stateProvider
.state('app', {
url: "/",
views: {
"content": {templateUrl: "views/front/home-1.0.html"}
}
})
.state('app.front.signin', {
url: "/signin",
views: {
"content": {templateUrl: "views/front/home-1.0.html", controller: "signinCtrl"}
}
});
}
]);
Can someone help me ?
You messed up in type it should be ui-sref instead of ui-shref
<body>
<h1>App</h1>
<nav>
<a ui-sref="app">{{link.home}}</a>
<a ui-sref="app.front.signin">{{link.signin}}</a>
</nav>
<div ui-view="content">
</div>
</body>
Your second link should be app.signin instead of app.front.signin because you don't have parent route front
.state('app.signin', {
url: "/signin",
views: {
"content": {
templateUrl: "views/front/home-1.0.html",
controller: "signinCtrl"
}
}
});