AngularJS ui router - not showing nested partials views - javascript

I have an AngularJs app with start up page as index.html, by default the projects view will be displayed and on top of the page I am showing a icon to show the todo items (for the logged-in user) which I am using bootstrap's data-toggle dropdown. The issue is whenever I click the todo link the partial view (todo.html) is not showing. BTW, I am new to the angular world so please forgive me if there is anything silly. Please see the code below:
Index.html
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head></head>
<body>
<a data-toggle="dropdown" class="dropdown-toggle" ui-sref=".todo">
<i class="icon-tasks"></i>
<span class="badge badge-grey">4</span>
</a>
<div ng-view></div>
</body>
app.js
// For any unmatched url, redirect to /projects
$urlRouterProvider.otherwise("/projects");
//
// Now set up the states
$stateProvider
.state('projects', {
url: "/projects",
templateUrl: "/app/views/projects/projects.html",
controller: "projectController"
})
.state('projects.todo', {
url: "/todo",
templateUrl: "/app/views/todo/todo.html"
});

First of all replace ng-view with ui-view in the root template, cause it seems you want to use ui-router instead of ng-router.
Wrap the content of your template files with div of ui-view as a parent element.
/app/views/projects/projects.html
/app/views/todo/todo.html
<div ui-view>
... previously defined content ...
</div>
Let's say your view was
<div class="container">
<div class="page-header">
<h1>Title: {{title}}</h1>
</div>
</div
you need to add ui-view to the div
<div class="container" ui-view>
<div class="page-header">
<h1>Title: {{title}}</h1>
</div>
</div
or wrap your view with div containing ui-view descriptor in case your vie contains several tags.
I cannot show you an example since you did not provide content of view files.
/app/views/projects/projects.html
/app/views/todo/todo.html
The issue is that after fist template applying angular does not see the place to put new template anymore.

ui-router isn't really supposed to be used in this way. To integrate bootstrap with angular you want to look at UI Bootstrap - http://angular-ui.github.io/bootstrap/
Then to achieve your drop down, look at their basic examples. If you want to use separate view files to define your drop down content, you can use <div ng-include="'mycontent.html'"></div>.
ui-router is useful if you have a complex view hierarchy, where you are for example, looking for dynamic loading of children, while keeping parent states static.

In ui-router you defined all of this in the $stateProvider, so there you should define that you have a view that has another view belonging to it, example:
<!-- In index.html the main view that will have all the page views-->
<div id="main" ui-view="main"></div>
<!-- In todo.html with a partial with the dropdown code in dropdown.html -->
<h1> This is a nice todo drop down </h1>
<div id="todoDropDown" ui-view="todoDropDown"></div>
//In your app file
.state('projects.todo', {
url: '/todo',
views: {
'main#': {
templateUrl: '/app/views/todo/todo.html',
controller: 'TodoCtrl'
},
'todoDropDown#projects.todo': {
templateUrl: '/app/views/partials/dropdown.html'
}
}
})
"todoDropDown#projects.todo" This does the magic, it tells that this view has another view inside. And you can add controller to it and all other options you have in ui-router. In this way you can break up as much as possible reusable parts.

Related

Should I use ng-app directive in every page for a non single page application?

I am developing a web application for a game using MVC but the views are different (there is a CreateGameView.html, a GameView.html ...) meaning there is not a shared navigation bar. I am using AngularJS.
When clicking some buttons in a page some Controller perform some action and then another View (html page) is loaded.
The question is should I use the ng-app directive in each html page using a Controller?
I would just use one ng-app directive. Instead use ngroute to control your view. There is a short tutorial on w3schools: https://www.w3schools.com/angular/angular_routing.asp
Below is the gist of it.
index.html
<body ng-app="myApp">
<!-- if you wanted a common navbar -->
<navbar></navbar>
<div ng-view></div>
</body>
starScreenView.html
<p>New Game</p>
<p>Load Game</p>
createGameView.html
<p>This will be the create game view</p>
<p>Main Menu</p>
<p>Load Game</p>
gameView.html
<p>This will be the game view</p>
app.js
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "startScreenView.html",
controller: "startScreenCtrl"
})
.when("/createGame", {
templateUrl : "createGameView.html",
controller: "createGameCtrl"
})
.when("/loadGame", {
templateUrl : "gameView.html",
controller: "gameViewCtrl"
})
});
What is happening is the ng-view is being replaced with the templateUrl html page, and this block has the controller wrapped around it.
For each routing end point you can have a page, controller, pass parameters around, etc. Just search google for ngroute examples.
If you must have separate HTML pages and you plan to use Angular on all of those pages then you need an ng-app on all those pages.
Otherwise use the router or craft your own way of swapping page content. If you page is simple enough you may not need the extra size of the router.

Load multiple views using angularjs

I have a scenario like this:
Link1
Link2
<div id="child1"></div>
<div id="child2"></div>
When I click on link1 I expect to load View A in div child 1 and View B in div child2.
When I click on link2 I expect to load View C in div child 1 and View D in div child2
I'm using ngRoute (standard AngularJS router) in my app.
i.e: I expect a different set of views for different links. How can this be accomplished using AngularJS. I understand that Angular does provide routing, but whatever examples I see online is only for a single view.
I have kept this scenario very simple. In reality it is a lot more complicated so I will not be able to combine 2 views into 1 for each link.
Here is a suggestion:
1) Make 4 templates (one per view):
viewA.html
viewB.html
viewC.html
viewD.html
2) Set 2 routes on your app (one per link):
$routeProvider.when('/page1', {
templateUrl: 'page1.html',
controller: 'Page1Ctrl'
}).when('/page2', {
templateUrl: 'page2.html',
controller: 'Page2Ctrl'
});
3) Include the views in page link1.html and link2.html
<!-- page1.html -->
<div id="child1" ng-include="'viewA.html'"></div>
<div id="child2" ng-include="'viewB.html'"></div>
<!-- page2.html -->
<div id="child1" ng-include="'viewC.html'"></div>
<div id="child2" ng-include="'viewD.html'"></div>
3) Set your <a> tags
Link1
Link2
Another suggestion, you can use ui-router and multiple named views it's easy to use and really powerful.
You can create your different view container using ui-view like this
<a href="#" >Link1</a>
<a href="#" >Link2</a>
<div ui-view="child1"></div>
<div ui-view="child2"></div>
And in you app.config, you can set for every states which template you want to load for the different views :
.state('report', {
views: {
'child1': { ... templates and/or controllers ... },
'child2': {}
}

Multiple views with angular

So I am trying to display multiple views in angular to help fix the footer problem I am having with this site I am building. I want to make sure that what I have been reading about and trying to mimic is making sense. This is what I have so far.
index.html
<!--Do not code below this line-->
<main ng-view="header"></main>
<main ng-view="body"></main>
<main ng-view="footer"></main>
routes.js file
angular.module('appRoutes', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
views: {
'header': {
temmplateUrl: 'app/views/header.html'
},
'body': {
templateUrl: 'app/views/body.html'
},
'footer': {
templateUrl: 'app/views/footer.html'
}
}
})
I have it working where I have just one view and have my header and footer inside the index.html file but I saw that you can have multiple views and really just switch out the "body" view with other pages.
Any advice would be much appreciated. Thank you
To display multiple views you could use only ng-include.
See https://docs.angularjs.org/api/ng/directive/ngInclude for more ngInclude details.
Here is a example: (notice the wrap in single quotes)
<div id="header">
<div ng-include="'header.html'"></div>
</div>
<div id="content" ng-view></div>
<div id="footer">
<div ng-include="'footer.html'"></div>
</div>
Use ngRoute with ng-view to define a region (e.g div#content) where will be changed the dynamic content (as partial html).
See https://docs.angularjs.org/api/ngRoute/directive/ngView for more ngRoute details.
Good luck!
You can have just one ng-view.
You can change its content in several ways: ng-include, ng-switch or mapping different controllers and templates through the routeProvider.
Alternatively, use ui-router

AngularJS ng-repeat display content in separate view

I'm working on a basic news feed app. What I need is for a specific news article, entry.content, to display in a separate view after a click. Both controllers have access to the data, but I need a synchronized click event so the second controller knows which specific article to display. I've been working on this for awhile & couldn't find relevant links on google or here where a secondary view was involved. I need a separate view b/c I have to add a lot more HTML to that page & eventually click(previous/next) between articles. I have an example which is working where the click event happens, but the content displays in the current view then hides after click.
I have a Plunker: http://plnkr.co/edit/lk66pRb7A6DkGM7NQKF7?p=preview
Here is the index.html code:
<body ng-app="FeedApp">
<h1>News Feed</h1>
<div ng-controller="NewsCtrl">
<div id="main">
<div ng-view=""><!-- angular yield --></div>
</div> <!-- main -->
</div> <!-- NewsCtrl -->
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="main.js"></script>
</body> <!-- FeedApp -->
Here is the home.html code:
<div ng-repeat="new in news">
<h3 ng-repeat="entry in new.entries" ng-if="$index < 3">
{{entry.title}}
</br>
Display this story</br>
Desired: click this Link, go to #/article & display this story
<p class="news-entries" ng-show="show" ng-bind-html="TrustSnippet(entry.content)"></p>
</h3>
</div> <!-- new in news -->
Currently, in the article.html file I only have a back button as I'm not sure what to put in there. All other code is in the Plunker, but I can add here if it will help.
I was thinking this might be be solved using current $index value, but I just don't know at this point.
Any help is appreciated.
You probably just want to use $routeParams like so:
here's a Plunker
route
.when("/article/:articleId", { . .
Link
<a href="#/article/{{$index}}" . . .
Param
FeedApp.controller("ArticlesCtrl", ["$scope", "$routeParams", '$http',
function($scope, $routeParams, $http) {
var index = $routeParams.articleId;
Article Object
$scope.article = $scope.news[0].entries[index];
I would omit the entries bit altogether and I would probably use a factory to manage/maintain the data.

Two ui-view in the same page AngularJs

I'm new in AngularJs and I have a problem,I have two ui-view in the same page, and when push in the one button for example for show the view goals, appear the form goal in the ui-view 1 and also in the second ui-view at the same time,I worked with states.I don't know how to do for when push one button only show the form belonging to one ui-view,
look my source:
HTML
<button class="btn btn-success"
ng-click="go('projectShow.milestones.reports')">New Report</button>
<div ui-view class="crearreport"></div>
<button class="btn btn-success"
ng-click="go('projectShow.milestones.goals')">New Goals</button>
<div ui-view class="creargoal"></div>
Controllerjs:
$scope.go = function(route){ $state.go(route); };
RouteJs:
.state('projectShow.milestones.goals', {
templateUrl: '../assets/projects/formulario.html', })
.state('projectShow.milestones.reports',{
templateUrl: '../assets/reports/formularioReport.html' })
Regards! and thanks for your time !
Since you have multiple ui-view directives you need to give each one a name so you can target them individually.
According to the ui-router wiki
You can only have one unnamed view within any template (or root html)
<!-- Unnamed -->
<div ui-view></div>
<!-- Named -->
<div ui-view="viewName"></div>
There are more details on how to setup ui-router & ui-view here.
https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-view

Categories