Not able to replace the view with <ion-nav-view> - angularjs

Not able to replace the view in ionic with . I have menu in form of buttons when I click them the view is not getting replaces.Below is the code I have tried
**index.html**
<div class="button-bar">
<a class="button button-small button-stable button-outline" ui-sref="page1">Page1</a>
<a class="button button-small button-stable button-outline" ui-sref="page2">Page2</a>
<a class="button button-small button-stable button-outline" ui-sref="page3">Page3</a>
</div>
<ion-nav-view name="page1"></ion-nav-view>
<ion-nav-view name="page2"></ion-nav-view>
<ion-nav-view name="page3"></ion-nav-view>
The js files used to replace the view.In page.controller.js nothing is theres. Just declared with app.controller and nothing is there.
**pages.js**
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state('page1', {
cache: false,
parent:'app',
views: {
'page1': {
templateUrl: 'templates/page1.html',
controller: 'pages.controller'
}
}
}).state('page2', {
cache: false,
parent:'app',
views: {
'page2': {
templateUrl: 'templates/page2.html',
controller: 'pages.controller'
}
}
}).state('page3', {
cache: false,
parent:'app',
views: {
'page3': {
templateUrl: 'templates/page1.html',
controller: 'pages.controller'
}
}
})
});
The below are the html pages for the views.
page1.html
<ion-view view-title="page1">
<ion-content>
<div> <h2> Welcome to Page 1</h2> </div>
</ion-view>
</ion-content>
page2.html
<ion-view view-title="page2">
<ion-content>
<div> <h2> Welcome to Page 2</h2> </div>
</ion-view>
</ion-content>
page3.html
<ion-view view-title="page3">
<ion-content>
<div> <h2> Welcome to Page 3</h2> </div>
</ion-view>
</ion-content>
app.js
var app = angular
.module(
'sampleapp',
[ 'ionic' ]).config(
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
cache : false,
url : "/app",
abstract:true,
templateUrl : "templates/index.html",
controller : 'appcontroller'
})
Kindly, help me understand why the view is not getting replaced.

this is likely because you have 3 different ion-nav-view tags, and each one of them having a separate name, along with each state only replacing its specific tag name.
Which means even if your views are injected, they will overlap (and probably why you're not seeing your views change, as theyre overlapping).
You should change:
index.html
<div class="button-bar">
<a class="button button-small button-stable button-outline" ui-sref="page1">Page1</a>
<a class="button button-small button-stable button-outline" ui-sref="page2">Page2</a>
<a class="button button-small button-stable button-outline" ui-sref="page3">Page3</a>
</div>
<ion-nav-view name="page-content"></ion-nav-view>
pages.js
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state('page1', {
cache: false,
parent:'app',
views: {
'page-content': {
templateUrl: 'templates/page1.html',
controller: 'pages.controller'
}
}
}).state('page2', {
cache: false,
parent:'app',
views: {
'page-content': {
templateUrl: 'templates/page2.html',
controller: 'pages.controller'
}
}
}).state('page3', {
cache: false,
parent:'app',
views: {
'page-content': {
templateUrl: 'templates/page1.html',
controller: 'pages.controller'
}
}
})
});

Related

Ionic button ng-click

I do have a different button on my first page.I just want to move to respective page on clicking to each button.However this is not working.I am new to ionic. Plz help.
Here is my controller.js
`angular.module('starter.controllers', [])
.controller('TutorialsCtrl', function($scope) {})
.controller('AndroidCtrl', function($scope,$state) {
$scope.create = function () {
$state.go('templates/android');
};
});`
This is my app.js
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('tab', {
url: '/tab',
abstract: true,
templateUrl: 'templates/tabs.html'
})
// Each tab has its own nav history stack:
.state('tab.tutorials', {
url: '/tutorials',
views: {
'tab-tutorials': {
templateUrl: 'templates/tutorials.html',
controller: 'TutorialsCtrl'
}
}
})
.state('tab.android',{
url:'/tutorials/:tutorialId',
views:{
'tab-tutorials':{
templateUrl:'templates/android.html',
controller:'AndroidCtrl'
}
}
})
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/tutorials');
});
here is my tutorials.html page
<ion-view view-title="Tutorials">
<ion-content class="padding">
<div class="background">
<button class="button button-outline button-positive myandroid" ng-click="create()">
<i class="icon ion-social-android home"></i>
<p>Android</p>
</button>
</div>
</ion-content>
</ion-view>
I want to navigate from tutorials.html to android.html.I do have a blank android.html page at this instant
Instead of button use a(anchor) as you wanted to change the state(route) of your application. And on each anchor you could use ui-sref directive with respective stateName, so that will create create a required href tag over there.
Markup
<ion-view view-title="Tutorials">
<ion-content class="padding">
<div class="background">
<a class="button button-outline button-positive myandroid" ui-sref="tab.tutorials">
<i class="icon ion-social-android home"></i>
<p>Android</p>
</button>
</div>
</ion-content>
</ion-view>
With $state.go() you can go to a state, not to a template as you did.
$scope.create = function () {
$state.go('tab.android');
};

Ionic view title not updating on transition to 'nephew' state

Basically I am trying to transition from my root.home state to root.topic.section (a topic view with a nested section view). The view loads but my back button and view title do not appear. The view title stays the same as it was in root.home. I don't understand because when i use ui-sref to change to sibling state (with no child states) then it DOES change the title.
EDIT: When I navigate from root.home to a sibling page root.dbtest, dbtest created a new navbar element in the DOM with the correct title, and sets the home's navbar to 'cached'. But when I navigate from root.home to root.topic.section no new DOM element is created and home remains active.
EDIT 2 this is the 'ui-sref' i am using to link to the sub state from root.home.
<a ui-sref="root.topic.section({topicId: xxx, inStore: false, topicName: xxx, sectionType: SECTION_TYPE.Summary})">link</a>
.
$stateProvider
.state('root', {
url: '/root',
abstract: true,
templateUrl: 'templates/menu-static.html',
controller: 'MenuCtrl'
})
.state('root.home', {
url: '/home',
views: {
'menuContent': {
templateUrl: 'templates/home.html',
controller: 'HomeCtrl'
}
}
})
.state('root.topic', {
url: '/topic/:topicId/{inStore}',
abstract: true,
cache: false,
views: {
'menuContent': {
templateUrl: 'templates/topic-view.html',
controller: 'TopicCtrl'
}
},
params: {topicName: null}
})
.state('root.topic.section', {
url: '/section/:sectionType',
views: {
'sectionSpace': {
templateUrl: 'templates/topic-section-view.html',
controller: 'TopicSectionCtrl'
}
}
})
Here is a snippet from my menu-static.html
<ion-side-menu-content>
<ion-nav-bar class="bar-stable">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="right">
<button class="button button-icon button-clear ion-navicon" menu-toggle="right">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent">
</ion-nav-view>
</ion-side-menu-content>
Here is a snippet from home.html
<ion-view view-title="All Topics">
<ion-nav-title>All Topics</ion-nav-title>
<ion-content>
</ion-content>
</ion-view>
Here is a snippet from topic-view.html
<ion-view view-title="NOT SHOWING">
<ion-nav-title>NOT SHOWING</ion-nav-title>
<ion-content>
<ion-nav-view name="sectionSpace">
</ion-nav-view>
</ion-content>
</ion-view>
Also: the Url will currently be /root/topic/... This seems undesirable=> you can have root have the url: '', and this will allow the url to be /topic/
The above did not work well for me so I tried this personally and it did work for the Parent:
Topic-view.html
<ion-view view-title="NOT SHOWING">
<div class="tabs-striped tabs-top tabs-background-balanced tabs-color-light">
<div class="tabs">
<a ng-repeat='section in ["Section1", "Section2", "Section3"]' class="tab-item" ng-click='goTo(section.toLowerCase())'>
{{section}}
</a>
</div>
</div>
<ion-nav-view name='sectionSpace'></ion-nav-view>
</ion-view>
And it did work.
I'm not sure what this {inStore} is about but maybe that's for earlier versions of Ionic/Angular I think I got this working in this codepen: http://codepen.io/zargold/pen/qZNJNJ?editors=1011

Adding slideshow to the ionic side menu starter app is not working

I am a novice to ionic and AngularJS so suspect I am doing something stupid but can't for the life of me work out what. I am using the ionic starter template for a side menu app. I have added another menu item for a 'new home' page. Into this page I have added some code from the ionic code pen example for an introduction slide show. Unfortunately I only get an empty page rather than the slide show.
My home.html template looks like this:
<script id="templates/home.html" type="text/ng-template">
<ion-view view-title="Intro">
<ion-nav-buttons side="left">
<button class="button button-positive button-clear no-animation" ng-click="startApp()" ng-show="!slideIndex">
Skip Intro
</button>
<button class="button button-positive button-clear no-animation" ng-click="previous()" ng-show="slideIndex > 0">
Previous Slide
</button>
</ion-nav-buttons>
<ion-nav-buttons side="right">
<button class="button button-positive button-clear no-animation" ng-click="next()" ng-show="slideIndex != 2">
Next
</button>
<button class="button button-positive button-clear no-animation" ng-click="startApp()" ng-show="slideIndex == 2">
Start using MyApp
</button>
</ion-nav-buttons>
<ion-slide-box on-slide-changed="slideChanged(index)">
<ion-slide>
<h3>Thank you for choosing the Awesome App!</h3>
<div id="logo">
<img src="http://code.ionicframework.com/assets/img/app_icon.png">
</div>
<p>
We've worked super hard to make you happy.
</p>
<p>
But if you are angry, too bad.
</p>
</ion-slide>
<ion-slide>
<h3>Using Awesome</h3>
<div id="list">
<h5>Just three steps:</h5>
<ol>
<li>Be awesome</li>
<li>Stay awesome</li>
<li>There is no step 3</li>
</ol>
</div>
</ion-slide>
<ion-slide>
<h3>Any questions?</h3>
<p>
Too bad!
</p>
</ion-slide>
</ion-slide-box>
</ion-view>
My app.js looks like this:
angular.module('starter', ['ionic', 'starter.controllers'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
// :NEW: Added stuff to show a home page
.state('app.home', {
url: '/home',
views: {
'menuContent': {
templateUrl: 'templates/home.html',
controller: 'IntroCtrl'
}
}
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html'
}
}
})
.state('app.browse', {
url: '/browse',
views: {
'menuContent': {
templateUrl: 'templates/browse.html'
}
}
})
.state('app.playlists', {
url: '/playlists',
views: {
'menuContent': {
templateUrl: 'templates/playlists.html',
controller: 'PlaylistsCtrl'
}
}
})
.state('app.single', {
url: '/playlists/:playlistId',
views: {
'menuContent': {
templateUrl: 'templates/playlist.html',
controller: 'PlaylistCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/playlists');
});
My controllers.js looks like this:
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
console.log('Doing login', $scope.loginData);
// Simulate a login delay. Remove this and replace with your login
// code if using a login system
$timeout(function() {
$scope.closeLogin();
}, 1000);
};
})
.controller('IntroCtrl', function($scope, $state, $ionicSlideBoxDelegate) {
$scope.next = function() {
$ionicSlideBoxDelegate.next();
};
$scope.previous = function() {
$ionicSlideBoxDelegate.previous();
};
// Called each time the slide changes
$scope.slideChanged = function(index) {
$scope.slideIndex = index;
};
})
.controller('PlaylistsCtrl', function($scope) {
$scope.playlists = [
{ title: 'Reggae', id: 1 },
{ title: 'Chill', id: 2 },
{ title: 'Dubstep', id: 3 },
{ title: 'Indie', id: 4 },
{ title: 'Rap', id: 5 },
{ title: 'Cowbell', id: 6 }
];
})
.controller('PlaylistCtrl', function($scope, $stateParams) {
});
The menu.html looks like this:
<ion-side-menus enable-menu-with-back-views="false">
<ion-side-menu-content>
<ion-nav-bar class="bar-stable">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left">
<ion-header-bar class="bar-stable">
<h1 class="title">Left</h1>
</ion-header-bar>
<ion-content>
<ion-list>
<ion-item menu-close ng-click="login()">
Login
</ion-item>
<ion-item menu-close href="#/app/search">
Search
</ion-item>
<ion-item menu-close href="#/app/browse">
Browse
</ion-item>
<ion-item menu-close href="#/app/playlists">
Playlists
</ion-item>
<!-- :NEW: Added New Home menu item -->
<ion-item menu-close href="#/app/home">
New Home
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>
When I run this app, open the menu, and select the New Home menu option the home.html page does not show properly, it is blank. I am sure I must be doing something stupid here.
I found my stupidity!
I had copied a line from the codepen example that wasn't needed and broke my code. Removing the following code from the home.html fixes the problem.
<script id="templates/home.html" type="text/ng-template">

Ionic nested views load on each other

Here's my Plunker .
.state('tabs', {
url: "/tab",
abstract: true,
cache: false,
templateUrl: "tabs.html"
// controller: 'AppCtrl'
})
.state('tabs.home', {
url: "/home",
cache: false,
views: {
'home-tab': {
templateUrl: "home.html"
// controller: 'welcomeController'
}
}
})
.state('tabs.list', {
url: "/list",
cache: false,
views: {
'category-tab': {
templateUrl: "list.html"
//
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/home');
Now when i switch between views , views load on each other and you can see both of them . or better to say : Url change but views don't change.
Am i missing something ? Thanks
Seems like you have forgort to wrap you list.html content inside ion-view
List.html
<ion-view>
<ion-header-bar align-title="center" class="bar-royal">
<h1 class="title">
list
</h1>
</ion-header-bar>
<ion-content class="">
<button class="button btn-custom button-full button-assertive icon icon-left ion-android-bicycle no-border red-custom no-margin">
Sport
</button>
</ion-content>
</ion-view>
Demo here

Ionic and Angular: Routing not working

I have an Ionic sidemenu project with the following menu:
<ion-side-menus enable-menu-with-back-views="false">
<ion-side-menu-content>
<ion-nav-bar class="bar-stable" align-title="center">
<ion-nav-back-button>
</ion-nav-back-button>
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left">
</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-nav-view name="menuContent"></ion-nav-view>
</ion-side-menu-content>
<ion-side-menu side="left">
<ion-content>
<div class="list">
<a class="item item-icon-left" menu-close href="#/app/products">
<i class="icon ion-home"></i>
<h2>Home</h2>
</a>
<a class="item item-icon-left" menu-close href="#/app/account">
<i class="icon ion-person"></i>
<h2>mein Konto</h2>
</a>
<a class="item item-icon-left" menu-close href="#/app/orders">
<i class="icon ion-android-list"></i>
<h2>meine Bestellungen</h2>
</a>
</div>
</ion-content>
</ion-side-menu>
</ion-side-menus>
The hrefs defined here are all working as intended.
My products page now has several sub-pages.
Html-File:
<ion-view view-title="Home">
<ion-content>
<ion-list>
<ion-item ng-repeat="product in products" href="#/app/products/{{product.templateUrl}}">
{{product.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
JS-File:
angular.module('App.Products', ['App.Products.Prints', 'App.Products.Box', 'App.Products.Book', 'App.Products.Framed'])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('app.products', {
url: "/products",
views: {
'menuContent': {
templateUrl: "modules/products/products.html",
controller: 'ProductsController'
}
}
});
})
.controller('ProductsController', function ($scope, ProductsFactory) {
$scope.products = [];
init();
function init() {
$scope.products = ProductsFactory.getProducts();
}
})
.factory('ProductsFactory', function() {
var products = [
{
name: 'Prints',
img: 'img/wardwarz.png',
templateUrl: 'prints'
},
{
name: 'PhotoBox',
img: 'img/argo.png',
templateUrl: 'box'
},
{
name: 'Photobuch',
img: 'img/django.png',
templateUrl: 'book'
},
{
name: 'Photo im Rahmen',
img: 'img/ic_profile.png',
templateUrl: 'framed'
}
];
var factory = {};
factory.getProducts = function () {
// Hier könnte ein HTTP Request rein um die Produkte vom Server zu erhalten!
return products;
}
return factory;
});
and to show one example one content of a sub page:
Html-File:
<ion-view view-title="Prints">
<ion-content>
</ion-content>
</ion-view>
JS-File:
angular.module('App.Products.Prints', [])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('app.products.prints', {
url: "/products/prints",
views: {
'menuContent': {
templateUrl: "modules/products/prints/prints.html"
}
}
});
});
When i now start the app ionic serve the navigation of the sidemenu works fine and i can open my products page. The navigation to prodcuts/prints does not work. I dont get an error message or anything. There is just nothing happening after the click on the item.
Whats going worng here?
An example Project can be found here: Mega File - MyApp.rar
Instead of using only href to do your navigation use
ui-sref="name_of_the_state"
Example:
ui-sref="app.products.prints"
This way you will not have issues with your navigation on angular/ionic.
If you even have a parameter in your route, like:
$stateProvider.state('app.products.edit', {
url: "/products/edit/:id",
views: {
'menuContent': {
templateUrl: "modules/products/prints/prints.html"
}
}
});
you can use:
ui-sref="app.products.edit(1)"
Also, I'm not seeying an abstract route on this, the app. It should be like:
app -> abstract
app.products
app.products.prints
I was able to simulate the error on my PC.
When you work with nested states you like app.products.print you need a intermediary view like this:
Example from a code I have on my pc that is working:
.state("admin", {
url: "/admin",
abstract: true,
views: {
"body": { templateUrl: "partials/_layoutAdmin.html" }
}
})
.state("admin.page", {
url: "/page/:idEdition",
abstract: true,
views: {
"header": { templateUrl: "partials/header.html", controller: "DefaultHeaderController" },
"content": { templateUrl: "partials/_layoutAdminEdition.html" }
}
})
.state("admin.page.create", {
url: "/create",
views: {
"content": { templateUrl: "partials/admin.page.form.html", controller: "PageFormController" }
}
})
Example of Templates
The main layout has thi
<div data-ui-view="body"></div>
The _layoutAdmin has this one
<header data-ui-view="header"></header>
<div class="container-fluid">
<div class="row">
<div data-ui-view="content"></div>
</div>
</div>
When the code enters the admin state it will load this first piece...
THEN...
<div data-ui-view="content" class="edition-form"></div>
AND THEN... the form CODE
So, in your case, you have app.products.prints:
app -> need a view -> ok you got it
app.products -> also need a view -> ok you got it
app.products.prints -> inside app.products view (products.html) you need a to show this inside it.
For simplicity, I suggest you to change it's name to app.productsPrint (without the "." and you'll be able to do it the way you are doing it right now with no impact.
Made some changes on the code you provided:
<ion-view view-title="Home">
<ion-content>
<div ui-view="myTestContent"></div>
</ion-content>
</ion-view>
Notice the ui-view="myTestContent"
and
.state('app.products.prints', {
url: "/prints",
views: {
'myTestContent': {
templateUrl: "modules/products/prints/prints.html"
}
}
});
and it worked.
So, in your case I recommend you to use productPrints instead of products.print. This nesting is used in those view inside view scenarios.

Resources