Angular ui-router having sticky views with nested views - angularjs

I have a Shell state with two nested states Home and About. I wish to be able to open a Modal state from either Home or About without Home or About being refreshed or removed from the screen, as is the case now. How can I amend to get this working?
http://plnkr.co/edit/EWDN3mmd4Nw0ATITrep4?p=preview
<div class="container">
<div ui-view></div>
</div>
<script type="text/ng-template" id="/shell.html">
<ul class="nav nav-pills">
<li>Home</li>
<li>About</li>
<li><a class="btn btn-default" ui-sref="shell.modal" data-toggle="modal" data-target="#modal">Open Modal</a></li>
</ul>
<div ui-view="main"></div>
<div class="modal fade" id="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</a>
</div>
<div class="modal-body">
<div ui-view="modal"></div>
</div>
</div>
</div>
</div>
</script>
<script type="text/ng-template" id="/home.html">
<h1>Home</h1>
<p>{{now}}</p>
</script>
<script type="text/ng-template" id="/about.html">
<h1>About</h1>
<p>{{now}}</p>
</script>
<script type="text/ng-template" id="/modal.html">
<h1>Modal content</h1>
<p>{{now}}</p>
</script>
<script>
var modalApp = angular.module('modalApp', ['ui.router']);
modalApp.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('shell', {
templateUrl: '/shell.html',
controller: function($scope){
$scope.now = Date.now();
}
})
.state('shell.home', {
url: '/home',
views: {
'main#shell': {
templateUrl: '/home.html',
controller: function($scope){
$scope.now = Date.now();
}
}
}
})
.state('shell.about', {
url: '/about',
views: {
'main#shell': {
templateUrl: '/about.html',
controller: function($scope){
$scope.now = Date.now();
}
}
}
})
.state('shell.modal', {
views: {
"modal#shell": {
templateUrl: '/modal.html',
controller: function($scope) {
$scope.now = Date.now();
}
}
}
});
});
</script>
(I do not want to use the $modal service from Angular UI as I have similar nested view requirements that are not using modals.)

Related

Route in the modal does nt work angularjs

I have a problem with my route in modal, I can retrieve my first route ("/",menu.html) but when I press the buttons, the other routes do not appear.
This is my route:
angular.module('ui.bootstrap.demo').config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state("foo", {
url: "/foo",
template: '<h1>foo</h1>'
})
.state("menu", {
url: "/",
templateUrl: 'menu.html'
})
.state("bar", {
url: "/bar",
template: '<h1>bar</h1>'
})
$urlRouterProvider.otherwise("/");
});
This is my modal with the div view:
<div class="modal-header">
<h3 class="modal-title" id="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body" id="modal-body">
<div class="row">
<div class="span12 ui-view-container">
<div class="well" ui-view></div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button>
<button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button>
</div>
And this is my menu.html:
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Quick Start</a>
<ul class="nav">
<li>Route 1</li>
<li>Route 2</li>
</ul>
</div>
</div>
I made a simple plunker with a modal.
State provider has ui-sref to handle route which takes the state name to navigate.
<ul class="nav">
<li><a ui-sref="foo">Route 1</a></li>
<li><a ui-sref="bar">Route 2</a></li>
</ul>
Update
When you open the modal, run it to a default state 'menu' through $state.go('menu').
In order for this to work, inject $state in the controller as bellow:
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl',
function ($uibModal, $log, $document, $state) {
......
$ctrl.open = function (size, parentSelector) {
$state.go('menu');
......
}
});
Plunker updated with comment query.

AngularJS routeProvider resource not find

I know that routeProvider topic has been raised over a thousand of times, but I still cannot figure out why my code isn't working. The problem is, I think, with routeProvider which shows Resource not found whenever i try to go to localhost:8080/login
Here is the code:
index.html
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Strona Logowania</title>
<link rel="stylesheet" type="text/css" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<div class="jumbotron">
<div class="container">
<div class="col-xs-offset-2 col-xs-8">
<div ng-view></div>
</div>
</div>
</div>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/angular-resource/angular-resource.min.js"></script>
<script src="../bower_components/angular-route/angular-route.min.js"></script>
<script src="../scripts/app.js"></script>
<script src="../scripts/controllers.js"></script>
<script src="../scripts/services.js"></script>
</body>
</html>
app.js
'use strict';
var app = angular.module('myApp', ['ngRoute', 'services', 'controllers']);
app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.
when('/login', {
controller: 'loginCtrl',
templateUrl: '../page/login.html'
}).
when('/home', {
controller: 'homeCtrl',
templateUrl: '../page/home.html'
}).
otherwise({
redirectTo: '/login'
});
}]);
controllers.js
'use strict';
var cont = angular.module('controllers', ['services']);
cont.controller('loginCtrl', ['$scope', 'usersFactory', '$window',
function ($scope, usersFactory, $window) {
$scope.login = function () {
usersFactory.create($scope.user).$promise
.then(function (response) {
$window.location.href = "/page/test.html";
}, function (response) {
$scope.errorMessage = "Bledne dane logowania";
});
}
}]);
cont.controller('homeCtrl', ['$scope', 'usersFactory', '$window']);
login.html
<div class="container">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" aria-selected="true">Davay bilet</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a class="nav dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">PL<span class="caret"></span></a>
<ul class="nav dropdown-menu">
<li>PL</li>
<li>EN</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div class="jumbotron">
<form name="myForm">
<div>
<label>Login*:</label>
<input type="text" ng-model="user.ldapLogin" name="ldapLogin"/>
</div>
<div>
<label>Hasło*:</label>
<input type="password" ng-model="user.password" name="password"/>
</div>
<button ng-click="login()" type="submit" class>Loguj</button>
</form>
</div>
When I insert login.html into index.html with added ng-controller it works fine.
Make sure you the templateUrl is set correctly. Relative paths in angular refer to the document in which the app is instantiated (i.e. index.html).
Assuming you have a structure like:
root/scripts/myApp.js
root/page/login.html
root/index.hmtl
the correct referral would be templateUrl: 'page/login', not as might be expected templateUrl: '../page/login'. According to angular there is no need to go one level down in your folder structure.
I think the templateUrl is relative to the current url but not the file system path because the view is loaded on client side. So I think the code below should work:
$routeProvider
.when('/login', {
controller: 'loginCtrl',
templateUrl: '/page/login.html'
})
.when('/home', {
controller: 'homeCtrl',
templateUrl: '/page/home.html'
})
.otherwise({
redirectTo: '/login'
});

AngularJS Modal Form Login

First of all , I m new in angularJS.
And I m using in my first App the following versions :
AngularJs 1.5
Bootstrap 3.3.6
my First AngularJS App is structued like the following :
index.html
----js:app.js
-------controllers:controller.js
-------services:service.js
:angular.js
:angular-route.js
:angular-animate.js
:ui-bootstrap-tpls-1.1.2.min.js
----html:home.html
:header.html
:footer.html
----modal:login.html
:register.html
----css:main.css
in index.html:
**<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<title>my First Angular App</title>
</head>
<body>
<div ng-view class="'slide-animation'"></div>
<script src="js/angular.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/angular-animate.js"></script>
<!-- <script src="js/angular-ui-bootstrap.js"></script> -->
<script src="js/ui-bootstrap-tpls-1.1.2.min.js"></script>
<script src="js/app.js"></script>
<!-- <script src="js/service/services.js"></script> -->
<script src="js/controller/controllers.js"></script>
<link href="css\bootstrap.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" type="text/css" href="css\mainstyle.css">
</body>
</html>**
app.js :
'use strict';
var app = angular.module('myApp', ['ngRoute','ngAnimate','ui.bootstrap']);
app.config(function($routeProvider){
//console.log($routeProvider);
$routeProvider
.when('/', { templateUrl: 'html/home.html', controller: "HomeCtrl"})
.when('/login', { templateUrl: 'modal/login.html', controller: "LoginCtrl"})
.when('/register', { templateUrl: 'modal/register.html', controller: "RegisterCtrl"})
.otherwise({redirecTo: '/'});
});
controller.js :
'use strict';
app.controller('HomeCtrl', function($scope,$rootScope){
//console.log($scope);
$scope.login=false;
$scope.slogan = "Jump inside AngularJS";
$rootScope.loading=false;
});
app.controller("LoginCtrl", function($scope,$uibModal,$log) {
$scope.open = function (size) {
console.log(size);
var modalInstance = $uibModal.open({
animate: true,
templateUrl: 'html/login.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
app.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
$scope.ok = function () {
$uibModalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
/*app.controller("modalAccountFormController", ['$scope', '$uibModal', '$log',
function ($scope, $uibmodal, $log) {
console.log('LoginCtrl');
//$scope.showForm = function (Type) {
// $scope.message = "Show Form Button Clicked:"+Type;
// console.log($scope.message);
var modalInstance = $uibModal.open({
templateUrl: 'modal4.html',
controller: ModalInstanceCtrl,
scope: $scope,
resolve: {
userForm: function () {
return $scope.userForm;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
//};
}]);*/
var ModalInstanceCtrl = function ($scope, $modalInstance, userForm) {
$scope.form = {}
$scope.submitForm = function () {
if ($scope.form.userForm.$valid) {
console.log('user form is in scope');
$modalInstance.close('closed');
} else {
console.log('userform is not in scope');
}
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
app.controller('RegisterCtrl', function($scope,$rootScope) {
console.log('RegisterCtrl');
});
home.html
<div>
<div id="wrapper">
<div class="container-fluid">
<header ng-include="'header.html'" ></header>
<div id="content">
<div class="row main-top-margin text-center">
<div class="col-md-8 col-md-offset-2 " >
<h1 class="animated flash">my First AngularJS App</h1>
<p>{{slogan}}</p>
</div>
</div>
</div>
<footer ng-include="'footer.html'"></footer>
</div>
</div>
</div>
header.html
<!-- Header -->
<div id="header">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<img src="img/headlogo.png" class="img-rectangle" alt="Logo" width="150" height="60">
</div>
<div>
<ul class="nav navbar-nav">
<li>Home</li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Info<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><span class="glyphicon glyphicon-info-sign"></span> About</li>
<li><span class="glyphicon glyphicon-envelope"></span> Contact</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a ng-show="login">Hello</a></li>
<li><span class="glyphicon glyphicon-log-out"></span> Logout</li>
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
</div>
And login.html
<div>
<modal title="Login form" visible="showModal">
<form role="form">
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" ng-model="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" ng-model="password"/>
</div>
<button type="button" class="btn btn-default" ng-click="submit()">Submit</button>
</form>
</modal>
</div>
And my problem is : myApp NOT working :-(
And I don't know how to solve or debug it .
Could you please tell first of all on what I have done is it the right way of handling with Angularjs 1.x ?
Second thing , How to solve my issue ?
Thank you .
/Koul

thinkster MEAN stack tutorial - ui router and inline template

I am working on thinkster 'Learn to Build Modern Web Apps with MEAN' tutorial. The tutorial worked well till the ui-router part. After coding the ui-router and using the inline template, my index.html is blank. googled quite a bit but I am not able to find anything helpful. Here's my code.
index.html
<html>
<head>
<title>Flapper News</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script src="app.js"></script>
<style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="flapperNews">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
<script type="text/ng-template" id="/home.html">
<div class="page-header">
<h1>Flapper News</h1>
</div>
<div class="page-header">
<h1>Flapper News</h1>
</div>
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span class="glyphicon glyphicon-thumbs-up"
ng-click="incrementUpvotes(post)"></span>
{{post.upvotes}}
<span style="font-size:20px; margin-left:10px;">
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
</span>
</div>
<form ng-submit="addPost()"
style="margin-top:30px;">
<h3>Add a new post</h3>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Title"
ng-model="title"></input>
</div>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Link"
ng-model="link"></input>
</div>
<button type="submit" class="btn btn-primary">Post</button>
</form>
</div>
</div>
</script>
</body>
</html>
app.js
angular.module('flapperNews', ['ui.router'])
var app = angular.module('flapperNews', []);
app.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home/',
templateUrl: '/home.html',
controller: 'MainCtrl'
})
.state('posts', {
url: '/posts/{id}',
templateUrl: '/posts.html',
controller: 'PostsCtrl'
});
$urlRouterProvider.otherwise('home');
}]);
app.factory('posts', [function(){
var o = {
posts: []
};
return o;
}]);
app.controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts){
$scope.test = 'Hello world!';
$scope.posts = posts.posts;
$scope.addPost = function(){
if(!$scope.title || $scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 0,
comments: [
{author: 'Joe', body: 'Cool post!', upvotes: 0},
{author: 'Bob', body: 'Great idea but everything is wrong!', upvotes: 0}
]
});
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
}]);
app.controller('PostsCtrl', [
'$scope',
'$stateParams',
'posts',
function($scope, $stateParams, posts){
}]);

How to be get HTML in <script type="text/ng-template"...> to display correctly in editors?

I created this AngularJS routing app which allows me to have all pages not in separate .htm files but all on one page with the individual page content in ng-template script elements.
Although this works, the problem is that NetBeans and PHPStorm don't recognize HTML within a script tag and so do not format it properly.
How can I change this so that I can edit this one file nicely in an editor?
<html ng-app="mainApp">
<head>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet" />
<style type="text/css">
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;
}
a:focus {
outline: none;
}
</style>
</head>
<body ng-cloak ng-controller="mainController">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<div class="navbar-brand">AngularJS Routing</div>
</div>
<div>
<ul class="nav navbar-nav">
<li><i class="fa fa-home"></i> Home</li>
<li><i class="fa fa-shield"></i> About</li>
<li><i class="fa fa-comment"></i> Contact</li>
</ul>
</div>
</div>
</nav>
<div class="col-lg-12">
<div ng-view></div>
</div>
<script type="text/ng-template" id="/home.htm">
<div class="jumbotron">
<h1>Home</h1>
<p>{{subtitle}}</p>
</div>
</script>
<script type="text/ng-template" id="/about.htm">
<div class="jumbotron">
<h1>About</h1>
<p>{{subtitle}}</p>
</div>
</script>
<script type="text/ng-template" id="/contact.htm">
<div class="jumbotron">
<h1>Contact</h1>
<p>{{subtitle}}</p>
</div>
</script>
<script>
var mainApp = angular.module('mainApp', ['ngRoute']);
mainApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/home.htm',
controller: 'mainController'
})
.when('/about', {
templateUrl: '/about.htm',
controller: 'aboutController'
})
.when('/contact', {
templateUrl: '/contact.htm',
controller: 'contactController'
})
.otherwise({
redirectTo: '/'
});
});
mainApp.controller('mainController', function ($scope) {
$scope.subtitle = 'the home page';
$scope.message = '';
$scope.$on("$destroy", function () {
myData.message = $scope.message;
});
});
mainApp.controller('aboutController', function ($scope) {
$scope.subtitle = 'the about page';
});
mainApp.controller('contactController', function ($scope) {
$scope.subtitle = 'the contact page';
});
</script>
</body>
</html>
With PHPStorm I belive the feature is called "language injection".
You can read about how to set it up here.
I'm not sure about netbeans, it may not have the feature.

Resources