I am having a problem using my angular js controller on more than one ons-template using split-view navigation.
On the main page it works well, but when i load the second ons-template (that uses the same controller) on the main page it does not work at all.
Please help.
`
<ons-split-view
var="app.splitView"
secondary-page="menu.html"
main-page="search.html"
main-page-width="70%"
collapse="width 500px">
</ons-split-view>
<ons-template id="menu.html">
<ons-page style="background-color: white; border-right: 1px solid #ddd;">
<ons-toolbar>
<div class="center">Menu</div>
</ons-toolbar>
<ons-list>
<ons-list-item
modifier="tappable"
ng-click="app.splitView.toggle(); app.splitView.setMainPage('search.html');">
<i class="ion-search" style="color: #666"></i>
Search
</ons-list-item>
<ons-list-item
modifier="tappable"
onclick="app.splitView.toggle(); app.splitView.setMainPage('mostWanted.html');">
<i class="ion-person-stalker" style="color: #666"></i>
Most Wanted
</ons-list-item>
<ons-list-item
modifier="tappable"
ng-click="app.splitView.toggle(); app.splitView.setMainPage('stolenVehicles.html');">
<i class="ion-model-s" style="color: #666"></i>
Stolen Vehicles
</ons-list-item>
</ons-list>
</ons-page>
</ons-template>
<ons-template id="search.html">
<ons-navigator var="searchNavigator">
<ons-page ng-controller="searchCtrl">
<ons-toolbar>
<div class="center">
<i class="ion-search" style="color: #666"></i>
Search
</div>
</ons-toolbar>
<div style="text-align: center; padding: 60px 10px 10px 10px;">
<section style="padding: 8px">
<b>Enter ID Number</b>
</section>
<section style="padding: 0 8px 8px">
<input type="search" class="search-input" placeholder="Enter ID Number..." style="color:black" ng-model="idNumber"></input>
</section>
<section style="padding: 0 8px 8px">
<ons-button modifier="large" ng-click="getRecords();">Submit</ons-button>
</section>
</div>
</ons-page>
</ons-navigator>
</ons-template>
<ons-template id="record.html">
<ons-navigator var="recordNavigator">
<ons-page ng-controller="searchCtrl">
<ons-toolbar>
<div class="center">
<i class="ion-clipboard" style="color: #666"></i>
Record
</div>
</ons-toolbar>
<div style="text-align: center; padding: 60px 10px 10px 10px;">
<h3>{{record.id}}</h3>
</div>
</ons-page>
</ons-navigator>
</ons-template>
`
JS:
app.controller('searchCtrl', function($scope,data){
$scope.record = null;
$scope.getRecords = function(){
for(var x=0; x<data.person.length; x++){
if($scope.idNumber == data.person[x].id){
$scope.record = data.person[x];
}
}
if($scope.record == null){
alert("No Criminal Record Found");
} else{
searchNavigator.pushPage('record.html');
//console.log($scope.record.id);
}
};
});
If you want the two pages to share the same controller, define the controller on the parent of the "ons-split-view" instead of each page. You can wrap the "ons-split-view" in a div.
For example;
<div ng-controller="searchCtrl">
<ons-split-view
var="app.splitView"
secondary-page="menu.html"
main-page="search.html"
main-page-width="70%"
collapse="width 500px">
</ons-split-view>
</div>
Related
I am trying to write a small Cordova App. Fortunatly found OnSenUI and now working on it. But I am facing this erorr:
Uncaught (in promise) Error: "html" must be one wrapper element.
at Object.util.createElement (util.js:147)
at page-loader.js:23
I searched for many solutions, but nothing worked for me.
My whole code:
<!DOCTYPE html>
<html>
<head>
<title>New App</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/onsenui.css" />
<link rel="stylesheet" href="css/onsen-css-components.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="js/onsenui.js"></script>
<script src="js/myJs.js"></script>
</head>
<body>
<ons-splitter>
<ons-splitter-side id="menu" side="left" width="220px" collapse swipeable>
<ons-page>
<ons-list>
<ons-list-item onclick="fn.load('home.html')" tappable> Home </ons-list-item>
<ons-list-item id="ons-list-fetch" onclick="fn.load('fetchPage.html');" tappable> Fetch Data </ons-list-item>
</ons-list>
</ons-page>
</ons-splitter-side>
<ons-splitter-content id="content" page="home.html"></ons-splitter-content>
</ons-splitter>
<ons-template id="home.html">
<ons-page id="helloworld-page">
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="fn.open()">
<ons-icon icon="md-menu"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center"> Main </div>
</ons-toolbar>
<p style="text-align: center; opacity: 0.6; padding-top: 20px;"> Swipe right to open the menu! </p>
<!-- Inputs-->
<h2 style="text-align: center; opacity: 0.6;">Just H2</h2>
<div style="text-align: center; margin-top: 30px;">
<p>
<ons-input id="username" modifier="underbar" placeholder="Username" float></ons-input>
</p>
<p>
<ons-input id="password" modifier="underbar" type="password" placeholder="Password" float></ons-input>
</p>
<p style="margin-top: 30px;">
<ons-button id="btnClickMe">Click Me!</ons-button>
</p>
<section style="padding: 8px">
<ons-button modifier="quiet">Forgot Password</ons-button>
</section>
</div>
<!-- eof Inputs-->
<p style="text-align: center; font-size: 14px; opicity: 0.6"> All rights reserved </p>
</ons-page>
</ons-template>
<!-- fetchPage-->
<ons-template id="fetchPage.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="fn.open()">
<ons-icon icon="md-menu"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center"> Fetch </div>
</ons-toolbar>
</ons-page>
<div id="divFtechHere">Fetched Data will be shown here</div>
</ons-template>
</body>
</html>
OMG Its too simple.
I was declaring <div id="divFtechHere"> outside <ons-page>
Wrong Code:
<!-- FetchPage-->
<ons-template id="fetchPage.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="fn.open()">
<ons-icon icon="md-menu"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center"> Fetch </div>
</ons-toolbar>
</ons-page>
<div id="divFtechHere">Fetched Data will be shown here</div>
</ons-template>
Correct Code:
<ons-template id="fetchPage.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="fn.open()">
<ons-icon icon="md-menu"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center"> Fetch </div>
</ons-toolbar>
<div id="divFtechHere">Fetched Data will be shown here</div>
</ons-page>
</ons-template>
In case in ons-sliding-menu, both Android(4.4 - 6.0) and iOS(9.3.1) text menu "Select All" don't work correctly.
("Copy", "Cut" and "Paste" work fine.)
See below with Android or iOS.
http://codepen.io/nf_takahashi/pen/oxqbJw
Is that a bug?!
ons.bootstrap();
<ons-sliding-menu main-page="page1.html" menu-page="menu.html" side="left" max-slide-distance="250px" var="menu">
</ons-sliding-menu>
<ons-template id="page1.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button ng-click="menu.toggleMenu()">
<ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">Page 1</div>
</ons-toolbar>
<p style="text-align: center; color: #999; padding-top: 100px;"><input type=text></p>
</ons-page>
</ons-template>
<ons-template id="page2.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="menu.toggleMenu()">
<ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">Page 2</div>
</ons-toolbar>
<p style="text-align: center; color: #999; padding-top: 100px;">Page2 Contents</p>
</ons-page>
</ons-template>
<ons-template id="menu.html">
<ons-list>
<ons-list-item modifier="chevron" onclick="menu.setMainPage('page1.html', {closeMenu: true})">
page1.html
</ons-list-item>
<ons-list-item modifier="chevron" onclick="menu.setMainPage('page2.html', {closeMenu: true})">
page2.html
</ons-list-item>
</ons-list>
</ons-template>
I would consider this a bug too, since the inputs should remain selectable regardless of the other components. Though it turns out it's created due to bad default values of a library Hammer.js which Onsen 1 uses.
Fixes:
You can remove that option from the default settings before calling ons.bootstrap().
delete Hammer.defaults.behavior.userSelect;
ons.bootstrap();
The other thing which you could do is just remove the disabling:
document.querySelector('ons-sliding-menu').onselectstart = document.querySelector('.onsen-sliding-menu__main').onselectstart = null;
Note that this fix must be executed after the sliding-menu is created/ready. So in your case just wrapping it in ons.ready(function(){ /* fix */ }) was enough.
If you are creating a page with a sliding menu later then you would need to wait for the page init for example and then apply the fix.
Here's a demo where you can see the two fixes in action.
Anyway the first fix is probably easier, but I just came up with the second one initially, so I decided to include that too.
I want some values from one ons-template to another, i have tried sending values from ons-template to controller and getting back to another ons-template from a controller, but is there any way i can get the values directly from one ons-template to another without calling the controller? Below is my code.
sendmoney.html
<ons-template id="sendmoney.html">
<ons-page modifier="shop-details" class='page_bg'>
<ons-toolbar style="background-color: #16A500;">
<div class="left">
<ons-toolbar-button ng-click="menu.openMenu();">
<ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">Send Money</div>
<div class="right">
<ons-toolbar-button onclick="doContactPicker()">
<ons-icon icon="ion-plus" size="28px" fixed-width="false"></ons-icon>
</ons-toolbar-button>
</div>
</ons-toolbar>
<div ng-controller="getUserContacts">
<ons-list-header>Select Contact</ons-list-header>
<ons-list style="margin: -1px 0">
<div ng-show="loading" class="loading"><img src="img/loading.gif" height="50px" width="50px"></div>
<ons-list-item modifier="chevron" class="list-item" ng-repeat="x in contacts track by $index" style="margin-left: 10px;" ng-click="page.pushPage('sendmoney2.html', {animation: 'slide'});">
<ons-row>
<ons-col width="52px" style="padding: 10px 0 0 0;">
<img src="img/red.png" height="42px" width="42px"/>
</ons-col>
<ons-col>
<header>
<span class="item-title">{{x.name}}</span>
</header>
<p class="swipe-item-desc">{{x.phone}}</p>
</ons-col>
</ons-row>
</ons-list-item>
</ons-list>
</div>
</ons-page>
</ons-template>
I want {{x.name}} and {{x.phone}} in sendmoney2.html template
sendmoney2.html
<ons-template id="sendmoney2.html">
<ons-page modifier="shop-details" class='page_bg'>
<ons-toolbar style="background-color: #16A500;">
<div class="left">
<ons-toolbar-button ng-click="menu.openMenu();">
<ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center">Send Money</div>
</ons-toolbar>
</ons-page>
</ons-template
I don't know Angular very well, but in plain JS, you simply set a global variable to get around this. I think of the app design as just one large web page. That often helps me solve my problems. So basically just define these in the top of your script:
var vName = '';
var vPhone = '';
Then set them in the events, this should work.
I want to add a page before my sliding menu page. I succeeded to accomplish that. But, now I can't access menu.toggle();
I logged the menu object, that says undefined. And I've also tried navi.menu but that also doesn't seem to work. This is the closest solution I've got but no menu.toggle
Onsen UI - Using Slide-Menu just for specifics pages here is my code
<ons-navigator title="Navigation" var="navi" page="homePage.html">
</ons-navigator>
<ons-template id="homePage.html">
<ons-page ng-controller="mainPageController">
<ons-toolbar>
<div class="center">PHPTRAVELS</div>
<div class="right">
<ons-toolbar-button>Exit</ons-toolbar-button>
</div>
</ons-toolbar>
<ons-row class="home-page">
<ons-col>
<ons-list>
<ons-list-item modifier="tappable"
style="line-height: 25px; font-size: 12px; padding: 5px;"
ng-show="modules[2].status"
ng-click="navi.pushPage('home.html')">
<ons-row>
<ons-col width="70px" class="ic">
<ons-icon icon="fa fa-building" size="50px"></ons-icon>
</ons-col>
<ons-col>
<div class="title">Hotels</div>
<div class="description">Find the perfect hotel room</div>
</ons-col>
</ons-row>
</ons-list-item>
<ons-list-item modifier="tappable" style="line-height: 25px; font-size: 12px; padding: 5px;"
ng-show="modules[6].status || modules[7].status">
<ons-row>
<ons-col width="70px" class="ic">
<ons-icon icon="fa fa-plane" size="50px"></ons-icon>
</ons-col>
<ons-col>
<div class="title">Flights</div>
<div class="description">Get the best flight</div>
</ons-col>
</ons-row>
</ons-list-item>
<ons-list-item modifier="tappable" style="line-height: 25px; font-size: 12px; padding: 5px;"
ng-show="modules[3].status">
<ons-row>
<ons-col width="70px" class="ic">
<ons-icon icon="fa fa-briefcase" size="50px"></ons-icon>
</ons-col>
<ons-col>
<div class="title">Tours</div>
<div class="description">Your vacation awaits</div>
</ons-col>
</ons-row>
</ons-list-item>
<ons-list-item modifier="tappable" style="line-height: 25px; font-size: 12px; padding: 5px;"
ng-show="modules[1].status">
<ons-row>
<ons-col width="70px" class="ic">
<ons-icon icon="fa fa-car" size="50px"></ons-icon>
</ons-col>
<ons-col>
<div class="title">Cars</div>
<div class="description">Seek a deal on wheels</div>
</ons-col>
</ons-row>
</ons-list-item>
<ons-list-item modifier="tappable" style="line-height: 25px; font-size: 12px; padding: 5px;"
ng-show="modules[0].status">
<ons-row>
<ons-col width="70px" class="ic">
<ons-icon icon="fa fa-leanpub" size="50px"></ons-icon>
</ons-col>
<ons-col>
<div class="title">Blog</div>
<div class="description">Travel news portal</div>
</ons-col>
</ons-row>
</ons-list-item>
</ons-list>
</ons-col>
</ons-row>
</ons-page>
</ons-template>
<ons-sliding-menu
menu-page="menu.html" main-page="home.html" side="left"
var="menu" type="reveal" max-slide-distance="260px">
</ons-sliding-menu>
<ons-template id="menu.html">
<ons-page modifier="menu-page">
<br><br>
<ons-list class="menu-list">
<ons-list-item menuitem class="menu-item selected" ng-click="menu.setMainPage('home.html', {closeMenu: true});">
<i class="glyphicon glyphicon-home"></i>
Home
</ons-list-item>
<ons-list-item menuitem class="menu-item" ng-click="menu.setMainPage('browsehotels.html', {closeMenu: true});">
<i class="glyphicon glyphicon-calendar"></i>
Browse Hotels
</ons-list-item>
<ons-list-item menuitem class="menu-item" ng-click="menu.setMainPage('specialoffers.html', {closeMenu: true});">
<i class="glyphicon glyphicon-gift"></i>
Special Offers
</ons-list-item>
<ons-list-item menuitem class="menu-item" ng-click="menu.setMainPage('blog.html', {closeMenu: true});">
<i class="glyphicon glyphicon-th-list"></i>
Blog
</ons-list-item>
<ons-list-item menuitem class="menu-item" ng-click="menu.setMainPage('mybookings.html', {closeMenu: true});">
<i class="glyphicon glyphicon-user"></i>
My Bookings
</ons-list-item>
<ons-list-item menuitem class="menu-item" ng-click="menu.setMainPage('contactus.html', {closeMenu: true});">
<i class="glyphicon glyphicon-map-marker"></i>
Contact Us
</ons-list-item>
</ons-list>
</ons-page>
</ons-template>
<ons-template id="home.html">
<!-- <ons-navigator var="nav"> -->
<ons-page ng-controller="homeController">
<ons-toolbar>
<div class="left">
<ons-toolbar-button ng-click="menu.toggle();">
<ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
</ons-toolbar-button>
</div>
<div class="center text-center">PHPTRAVELS</div>
<div class="right">
<ons-toolbar-button>
<!-- <ons-icon icon="refresh" size="30px" spin="{{isFetching}}" fixed-width="true"></ons-icon> -->
<ons-icon icon="ion-load-c" ng-show="isFetching" spin="{{isFetching}}"></ons-icon>
</ons-toolbar-button>
</div>
</ons-toolbar>
<div>some text</div>
</ons-page>
</ons-template>
The methods toggle(), open() and close() for the sliding menu are deprecated.
You can use the toggleMenu([options]) method instead: http://onsen.io/reference/ons-sliding-menu.html#method-toggleMenu
It seems like you want a page before the sliding menu so I made a small example:
http://codepen.io/argelius/pen/XJOQwy
I am trying to define event listener for ons-sliding-menu "preopen" event. I want setTitle() function of UICtrl controller to be invoked on preopen. Here's my code
app.js
app.controller('UICtrl', function($scope, UIService){
$scope.setMainTitle = function(title) {
UIService.setTitle(title);
}
}
I have tried the following HTML. But the event is not even fired ( i tried to write to console, it is not fired at all)
index.html
<div ng-controller="UICtrl" >
<ons-sliding-menu var="app.slidingMenu" ng-preopen="setMainTitle('test')"
menu-page="menu.html" main-page="main.html" swipable
side="left" type="reveal" max-slide-distance="250px">
</ons-sliding-menu>
</div>
another thing I tried was:
index.html
ons.ready(function() {
app.slidingMenu.on('preopen', function() {
console.log('preopen');
setMainTitle('Test');
} )
});
in this case the event is fired but setMainTitle function is naturally undefined in this scope.
Could someone have a suggestion how to achieve this?
I'm guessing the title is on your main.html??
I tried the same thing but without using angularJS
Look at this sample:
codepen example
JS:
ons.bootstrap();
ons.ready(function() {
menu.on("preopen", function() {
document.getElementById("title").innerHTML = "my title";
});
});
HTML:
<ons-sliding-menu
above-page="page1.html"
behind-page="menu.html"
side="left"
max-slide-distance="250px"
var="menu">
</ons-sliding-menu>
<ons-template id="page1.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button ng-click="menu.toggleMenu()"><ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon></ons-toolbar-button>
</div>
<div class="center" id="title">Page 1</div>
</ons-toolbar>
<p style="text-align: center; color: #999; padding-top: 100px;">Page1 Contents</p>
</ons-page>
</ons-template>
<ons-template id="page2.html">
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button onclick="menu.toggleMenu()"><ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon></ons-toolbar-button>
</div>
<div class="center">Page 2</div>
</ons-toolbar>
<p style="text-align: center; color: #999; padding-top: 100px;">Page2 Contents</p>
</ons-page>
</ons-template>
<ons-template id="menu.html">
<ons-list>
<ons-list-item modifier="chevron" onclick="menu.setAbovePage('page1.html', {closeMenu: true})">
page1.html
</ons-list-item>
<ons-list-item modifier="chevron" onclick="menu.setAbovePage('page2.html', {closeMenu: true})">
page2.html
</ons-list-item>
</ons-list>
</ons-template>