Angular-Fullstack + UI router isn't routing properly - angularjs

I'm attempting to route my application using UIrouter with the current yeoman-fullstack generator without mongoDB. In this example of my issue, everything is default to test and make sure the UI-router is working properly.
Mainly because I'm not understanding and missing something silly and noobish here, so I apologize.
https://github.com/DaftMonk/generator-angular-fullstack
I'm attempting to edit my routes in client/app/app.js using
'use strict';
angular.module('sr388App', [
'ngCookies',
'ngResource',
'ngSanitize',
'ui.router',
'ui.bootstrap'
])
.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
$urlRouterProvider
.otherwise('/');
$stateProvider
.state('about', {
url: '/about',
template: '<h1>Attempting to have this point at /main/about.html but this is for testing purposes</h1>'
});
$locationProvider.html5Mode(true);
});
Now, testing that, localhost:9000/about will load, but when I try to link the state to the navbar it breaks.
'use strict';
angular.module('sr388App')
.controller('NavbarCtrl', function ($scope, $location) {
$scope.menu = [{
'title': 'Home',
'link': '/',
'title': 'About',
'link': '/about'
}];
$scope.isCollapsed = true;
$scope.isActive = function(route) {
return route === $location.path();
};
});
The console error is
Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode
Here is the navbar html
<div class="navbar navbar-default navbar-static-top" ng-controller="NavbarCtrl">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" ng-click="isCollapsed = !isCollapsed">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
sr388
</div>
<div collapse="isCollapsed" class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav">
<li ng-repeat="item in menu" ng-class="{active: isActive(item.link)}">
<a ng-href="{{item.link}}">{{item.title}}</a>
</li>
</ul>
</div>
</div>
</div>
I'm certain my fundamental understanding of what's happening (supposed to be happening) is flawed.
I did notice the # is not being added when attempting to load states without changing URL.
<a ng-href="{{item.link}}">{{item.title}}</a>
I thought I had found the problem, and this would fix it
<a ui-sref="{{item.link}}">{{item.title}}</a>
But no dice.
Any advice/suggestions would be greatly appreciated. All I'm really trying to do is to create a simple SPA page based on this
http://i.imgur.com/sZYe15o.png
That would just simply run through a few different view states.
/#about
/#contact etc.

In ui-sref just put the state name instead of the URL/link
ui-sref="about"
Then it will work

Related

Angular.JS on Firefox Junk after document element error

When I test my index.html on Firefox with console.log it reads a strange error that says. I can't make the oage run on Google Chrome or IE.
junk after document element
This error is caused by the main.html page
<h1>This is my main</h1>
<h3>Scope value: {{ name }} </h3>
I'm just learning angular.js, in fact my code is exactly like this tutorial I was following, but I can't figure out why is not working. Can anyone point me in the right direction and tell me:
Why am I getting the console log error: Junk after document error, and
why can't I make ngview and templateURL work on my site?
Thanks!
My Plnkr Example
App.JS:
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'pages/main.html',
controller: 'mainController'
})
.when('/second', {
templateURL: 'pages/second.html',
controller: 'secondController'
})
});
myApp.controller('mainController', ['$scope', '$log', function($scope, $log) {
$scope.name = 'Main';
}]);
myApp.controller('secondController', ['$scope', '$log', function($scope, $log) {
$scope.name = 'Second';
}]);
Index.HTML
<header> <nav class="navbar navbar-default"> <div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">AngularJS</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><i class="fa fa-home"></i>Home</li>
<li><i></i>Second</li>
</ul> </div> </nav> </header>
<div class="container">
<div ng-view></div>
</div>
Main.HTML
<h1>This is my main</h1>
<h3>Scope value: {{ name }} </h3>
Second.HTML
<h1>This is my second</h1>
<h3>Scope value (on second page): {{ name }} </h3>
the code does not run on IE or Chrome cause they don't have a built in localhost feature. Firefox does.
Regarding the Error, I am not 100% sure but I believe that it has to do with Angular HTML format support in the browser itself. I am taking the same course now and facing the same issue. But the app is working fine except for those errors in Firefox.
Chrome: no errors.
MS Edge: no errors.
IE: no errors
===========================
update
Try using Brackets text editor, and use the Live Preview feature.
Open the Index.HTML page in Brackets and hit the button on the top right corner.

angular Js url navigation after getting rid of #

After reading this I was able to fix the # problem in my URL. It works fine without the URL.
In index.html
<base href="/app-name/">
My URL :
http://localhost:7001/app-name
Before applying base URL I had
http://localhost:7001/app-name/#/home
1) When I type this in browser how does the URL change to http://localhost:7001/app-name/home?
I was able to change and navigate to diff levels of application when using #. But, after applying base in index.html I am unable to perform this and the browser throws a 404. Any suggestions on how to get this working?
Here is my navigation page
<div class='navbar-header'>
<a class='navbar-brand' href="home"><img alt='img' src="appName/images/logo.PNG" type="image/png" height="30"></a>
<div class="navbar-header">
<a class="navbar-brand" href="home"><b>App - Name</b></a>
</div>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li> <span class="glyphicon glyphicon-home"></span> Home</li>
<li> <span class="glyphicon glyphicon-list-alt"></span> YYYYYYYYY </li>
<li> <span class="glyphicon glyphicon-hand-up"></span> XXXXXXXX</li>
</ul>
</div>
Router looks like this.
'use strict';
appName.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/home', {
templateUrl : 'appName/models/scheduler/home.html',
controller : 'mainController'
})
.when('/logs', {
templateUrl : 'appName/models/logs/logs.html',
controller : 'logsController'
})
.when('/info', {
templateUrl : 'appName/models/quick_info/info.html',
controller : 'yyyyController'
})
.otherwise({redirectTo:'/home'});
// $locationProvider.html5Mode(true);
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
Thanks for taking time to look into this.

Reusing a single modal to display dynamic content for a number of modules in angular js

I'm looking to create a stateful app that uses a common modal for a number of modules. On clicking items in a menu, the modal window should open and then I'm hoping to populate the modal with dynamic data based on the button that was pressed.
So for example, if I click 'Users' I would want the modal to pop up and then using angular, I want to populate the modal with all the users
I've been playing around but haven't managed to get very far with it.
Here is the body of my html:
<body ng-app="myApp">
<div class="collapse navbar-collapse navbar-center" id="js-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">
<a href="#/users" data-toggle="modal" data-target="#Modal">
<div class="users_icon"> </div>
<br>
<p>Users</p>
</a>
</li>
<li>
<a ng-href="#/settings" data-toggle="modal" data-target="#Modal">
<div class="settings_icon"> </div>
<br>
<p>Settings</p>
</a>
</li>
</ul>
</div>
<div class="modal fade" id="Modal" tabindex="-1" role="dialog" aria-labelledby="ModalLabel">
<div class="modal-dialog" role="document">
<form class="form-horizontal" role="form">
<div class="modal-content" ui-view="modal" autoscroll="false">
</div>
</form>
</div>
</div>
</body>
and my js so far:
'use strict';
angular
.module('myApp', [
'ui.router',
'js-data'
])
.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/calendar');
$stateProvider
.state("Modal", {
views:{
"modal": {
templateUrl: "views/modal.html"
}
},
abstract: true
})
.state("Modal.addtask", {
views:{
"modal": {
templateUrl: "views/modals/addtask.html"
}
}
});
})
I would like to be able to click an item in the menu, use the normal bootstrap javascript to open the modal dialog and then the modal to be populated by angular with dynamic data relative to the menu item clicked. I can't use angular ui as I am using SCSS for styling and it only supports LESS. Any help would be very appreciated.
At the moment, the modal pops up but then I can't get the data to populate in the modal
I managed to do this by utilising sticky states as described here: http://christopherthielen.github.io/ui-router-extras/#/sticky

AngularJS & Spring MVC - set templateUrl with $stateProvider

I am developing a web application using angular js & spring-mvc.
In the header of my application
header.jsp
<nav class="navbar navbar-default navbar-fixed-top" >
<div class="container">
<div class="navbar-header" >
<a class="navbar-brand"
href="<c:url value="/home"/>" >Pipeline Tracker
</a >
</div >
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li ui-sref-active="active">
<a ui-sref="empty">PIPELINE</a>
</li>
<li ui-sref-active="active">
<a ui-sref="employees">ADMIN</a>
</li>
</ul>
</div>
</div>
</nav>
app.js
app.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('empty', {
url: '/',
view: {
'main':{
templateUrl: 'views/templates/empty.html',
controller: 'EmptyController'
}
}
})
.state('employees',{
url: '/admin/employees',
view: {
'main':{
templateUrl: 'http://localhost:8070/PipelineClient/views/templates/employee.jsp',
controller: 'TestController'
}
}
});
$urlRouterProvider.otherwise('/');
});
I am unable to set the templateUrl property, so my app navigates perfectly. I can see url changing accordingly to '#/' and '#/admin/employees' but nothing is displayed. There is no error in the console also.
I tried to set templateUrl two ways. One with html and another with jsp using context url. None of them working.
I removed routing and checked the data loads pefectly. No issue with data loading.
for loading dependencies:
var app = angular.module('pipeline', [
'ngResource',
'infinite-scroll',
'angularSpinner',
'jcs-autoValidate',
'angular-ladda',
'mgcrea.ngStrap',
'toaster',
'ngAnimate',
'ui.router'
]);
index.jsp
<div class="container main-content">
<div ui-view="main"></div>
</div>
How to set this templateUrl property? I am relatively new to angular, so do let know if you need any more info on this.
I believe it's a syntax error, should be "views" and not "view".
.state('employees',{
url: '/admin/employees',
views: { <--- change view to views
'main':{
templateUrl: 'http://localhost:8070/PipelineClient/views/templates/employee.jsp',
controller: 'TestController'
}
}
});
I'm not familiar with the "view:" syntax you're using. does it work if you do:
.state("test", {
templateUrl: "views/templates/empty.html",
controller: 'EmptyController'
}
http://localhost:8070/#/test

Dynamically parsing HTML with AngularJS

I'm new to Angular and I have hit a small problem. I would like to set different menus depending on the user being authenticated or not. Depending on the status laravel returns different menu structures (html).
Here's my HTML for the main menu:
<div class="navbar navbar-default navbar-fixed-top" role="navigation" ng-controller="MenuCtrl">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src="/image">
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li ng-bind-html="firstoption" ng-class="{active:isActive('/firstoption')}">{{ firstoption }}</li>
<li ng-bind-html="secondoption" ng-class="{active:isActive('/secondoption')}" class="dropdown">
{{ secondoption }}
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
Heres my Controller:
The backend returns a json with the html. The reason why I want to do it like that has also to do with user rights. Depending on different rights, different options are shown in the menu.
AppControllers.controller('MenuCtrl', ['$scope', '$http', '$sce', '$location',
function ($scope, $http, $sce, $location) {
$scope.isActive = function(route) {
return route === $location.path();
}
$scope.firstoption = "";
$scope.secondoption = "";
var getMenuStructure = $http.get('/menustructure').success(function (data) {
$scope.firstoption = $sce.trustAsHtml(data.firstoption);
$scope.secondoption = $sce.trustAsHtml(data.secondoption);
});
}]);
It seems to work nearly perfectly, but the console still gives me an error:
TypeError: Object [object Object] has no method 'indexOf'
Is there a more elegant way of doing it? What am I doing wrong?
UPDATE
The problem seems to be the $sce.trustAsHtml();. When I remove it, the dropdowns don't work anymore and when I add them I get the error.
The ng-include allows you to include data from an external URL and compile it on the fly.
This will allow you to write code like this:
<ng-include src="/views/{{viewname}}.html"></ng-include>
and then in your controller, simply set (or change) the view by setting $scope.viewname, like so:
$scope.viewname = "view"
Even if you don't need to dynamically change which page is loaded, it's worth using the ng-include for simplicities sake.
I ran into this same problem. I found that $sce.getTrusted worked the same as trustAsHtml for my case. It looks like this: $sce.getTrusted($sce.HTML, someHtml)

Resources