Sorry about the title, not certain of a better subject line.
In my ionic project I have a popover window that I use alot but its repeated a lot through different pages. Currently, every popover is uniquely defined in every controller/page view....which means I need to edit every instance of the popover in every page if I want to change one element of the page.
To clean things up, I want to move the embedded script/template to an external file that would be called like:
$ionicPopover.fromTemplateUrl('templates/popover.html', {
scope: $scope,
animation: 'slide-in-up',
focusFirstInput: false
}).then(function(popover) {
$scope.popover = popover;
});
Easy enough...however, some popovers require custom HTML & custom $scope variables. Is it possible to define local <script> template code that can be passed into the external template? Something like:
Main template:
<ion-view id="view">
<ion-nav-title>
...
</ion-nav-title>
<ion-content id="thisContent">
<script id="popoverExtras.html" type="text/ng-template">
<div id="customDiv1"> {{data1}} .. some content .. </div>
<div id="customDiv2"> {{data2}} .. more content .. </div>
</script>
</ion-content>
</ion-view">
External Template:
<ion-popover-view>
<ion-header-bar>
<h1 class="title" style="text-align:center;">{{popTitle}} </h1>
</ion-header-bar>
<ion-content drag-content=“false”>
<div id="subject">{{popSubject}}</div>
<div id="message">{{popMessage}}</div>
<script id="popoverExtras.html" type="text/ng-template">
</script>
</ion-content>
</ion-popover-view>
And still load the entire external + local template into a single popover view with the standard load:
$ionicPopover.fromTemplateUrl('templates/popover.html', {
scope: $scope,
animation: 'slide-in-up',
focusFirstInput: false
}).then(function(popover) {
$scope.popover = popover;
});
Related
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.
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
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.
I don't know if my title describe well my problem but, it's what i think happen here. What am trying to do is to add facebook login to my app that i used the sidemenu ionic template and i followed the tutorial ionic facebook integration
It works well no issues on both browser and mobile but, when i tried to show the facebook data profile picture not in the template profile but, in the top of my side menu i got two issues.
First i need to refresh the browser so the picture show.
Second issue i get this alert Facebook error: undefined which is in the profileCtrl
i get this error when i created a div inside of the menu template and sat ng-controller="profileCtrl" here i think the app get a conflict because the APPCtrl is the controller of the menu Template like this:
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
here is the code i added to the menu template to show the picture and the user name in the top of the side menu:
<div ng-controller="ProfileCtrl" class="user-pro">
<img src="http://graph.facebook.com/{{user.id}}/picture?width=100&height=100"/>
<p class="text-center user-name side-btn">{{user.name}}</p>
</div>
and here is the menu template code and how i putted my code in it :
<ion-side-menus>
<ion-pane ion-side-menu-content>
<ion-nav-bar class="bar-stable nav-title-slide-ios7">
<ion-nav-back-button class="button-clear"><i class="icon ion-ios7-arrow-back"></i> Back</ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view name="menuContent" animation="slide-left-right"></ion-nav-view>
</ion-pane>
<ion-side-menu side="left">
<ion-content class="has-header side-bg"><!-- sidebar -->
<!-- user profile here -->
<div ng-controller="ProfileCtrl" class="user-pro">
<img src="http://graph.facebook.com/{{user.id}}/picture?width=100&height=100"/>
<p class="text-center user-name side-btn">{{user.name}}</p>
</div><!-- user Profile -->
<ion-list>
<ion-item>
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>
I wrapped the side menu template with a <div ng-controller="AppCtrl" and do the hierarchy explained here angular.js hierarchy controller guide and SURE i did deleted the controller: 'AppCtrl' from here
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
but it did't work.
sorry for being long.
I'm not sure I can answer all of your issues. I.e. The Facebook undefined error. However I can fix your photo not showing.
When using a binding in the src attribute of an image tag you should instead use ng-src.
Especially when the binding is changed asynchronously.
<img ng-src="http://graph.facebook.com/{{user.id}}/picture?width=100&height=100"/>
Using just src the browser would fetch something like "http://graph.facebook.com/UNDEFINED/picture?width=100&height=100" and when the value changes the browser will not check that and won't fetch the new image. Angular takes care of this for you. I'm guessing on refresh there is some sort of race condition that is being satisfied. Although I'm not familiar with how the Facebook library works.
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.