AngularJS ng-view Layout manage - angularjs

Hi I am working on a project using angularjs.
My roting logic is
.state( "authenticate", {
url: '/',
templateUrl : "views/login.html",
controller : "LoginCtrl",
data : {
requireLogin : false
}
})
.state( "beats", {
url: '/beats',
templateUrl : "views/beats.html",
controller : "BeatsCtrl",
data : {
requireLogin : true
}
})
.state( "outlets", {
url: '/outlets',
templateUrl : "views/beats.html",
controller : "BeatsCtrl",
data : {
requireLogin : true
}
})
I have ng-view in index.html that includes login.html first (url '/'). after login in each state I want to include a navbar in each template using ng-include.
currently I am writing in each page this line
<div ng-include=" 'templates/navbar.html' "></div>
but I want to include only once and each view should be render below it. But the problem is this navbar template itself getting render in ng-view.
and as far as I know we can not have multiple ng-view in one app.
can anyone please tell me what should be the best approach of doing this.

You could potentially have this nav bar included in the index.html itself, but hide it using ng-if or ng-show till the user is logged in. Once the user is logged in you could have a flag that evaluates to true and this nav bar can appear on every subsequent page.
in index.html
<div id="navbar" ng-if="isUserLoggedIn">
// nav bar code
</div>
And this 'isUserLoggedIn' will be set to true by your LoginCtrl on the scope.

Related

loading external URL on Dashboard Content

I want to load URL( external URL 'http://google.com') in Dashboard content,I tried with this code
.state('app.urlloading', {
url: '/url-loading',
controller:function($window){
$window.location.href = 'https://google.com';
},
templateUrl: 'views/tmpl/url-loading.html'
})
1) Is it possible to load a external URL in Dashboard content ?, If I click a button on sidebar of the dashboard only dashboard content should display the URL retaining sidebar and header, now whole dashboard is disappearing after clicking the button then redirecting to the google page.
What about using an iframe
url-loading.html
<iframe src="https://google.com"></iframe>
If you need to open differents url using same template & controller you can use a resolver
// loading google
.state('app.urlloading', {
url: '/url-loading-google',
controller:'UrlLoadingCtrl'
resolve {
url : function(){
return "https://www.google.com"
}
}
templateUrl: 'views/tmpl/url-loading.html'
})
// loading twitter
.state('app.urlloading', {
url: '/url-loading-twitter',
controller:'UrlLoadingCtrl'
resolve {
url : function(){
return "https://www.twitter.com"
}
}
templateUrl: 'views/tmpl/url-loading.html'
})
Controller
.controller('UrlLoadingCtrl', [ 'url', '$scope', function(url, $scope){
$scope.url = url;
})
url-loading.html
<iframe src="{{url}}"></iframe>
If you change location.href, whole page changes. Static HTML usable from states that static HTML link does not matter is external or not.
<div class="wrapper">
<div class="h_iframe">
<!-- a transparent image is preferable -->
<img class="ratio" src="http://placehold.it/16x9"/>
<iframe src="http://www.youtube.com/embed/WsFWhL4Y84Y" frameborder="0" allowfullscreen></iframe>
</div>
<p>Please scale the "result" window to notice the effect.</p>
But you could use iframes like this that is what are you exactly wanting.

angular ui router named view state overriding another named view

I have a project with 2 ui-view div:
<div ui-view='agent-station'></div>
<div ui-view='routingConfig'></div>
The main state login is successfully loaded in both:
$stateProvider
.state("login", {
url: "/login",
views: {
'agent-station': {
templateUrl: "login.html"
},
'routingConfig': {
templateUrl: "loginrc.html"
}
}
})
The problem arises when I click in one link which URL updates only one of the ui-views. The ui-view referenced by that URL is successfully updated, but the other ui-view becomes blank. So for example if I click on Test 1 AG link on the plunker example the Test 1 Agent station state is updated but the Login Routing Config state becomes a blank page.
My main suspicion is the following line:
$urlRouterProvider.otherwise("/login");
But I'm not sure why it becomes blank instead of showing the login page for Routing Config ui-view.
What could be the problem?
Here is my plunker:
Angular Named Views Test

Pass value to a template pages using Angular

I'm pretty new in Angular since today. :)
I've followed a lot of tutorials since this morning and for each one the same question from my side without answer.
I saw that my navigation could be as simple as the following:
<h3>HTML</h3>
<ul class="nav navbar-nav navbar-right">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
JS
scotchApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
But how it works if I need to page some value to a template page ?
And how can I get this passed value to use it in my template page ?
Thanks for your help.
Simply
define variable in controller $scope.variablename=value;
access in template as variablename inside double quotes or access as {{variablename}} outside the tag.
Don't forget to inject $scope dependency in controller.
Its all about using REST.
So lets say you want to pass a email id to your contact page, this is how your code would change.
//change in your html link. I have hard coded email for convenience. You could get it from $scope
<li>Contact</li>
Then you have to add the variables in your REST URL
//changes in your app.js
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html/:emailId',
controller : 'contactController'
});
And finally changes in your controller.
You'll have to add dependency for "$stateParams" and then the variable value will be available in your controller
scotchApp.controller ('contactController',['$stateParams', function($stateParams){
var emailId = $stateParams.emailId;
]})

How to load only required template in IonicFramework

I am getting two flow in my two diff codes.
I am developing an ionicframework based app using its default (angular based ui-router) routing.
Now when i coded for the ionic this way.
*.config(["$stateProvider","$urlRouterProvider",function ($stateProvider,$urlRouterProvider) {
$urlRouterProvider
.otherwise('oops');
$stateProvider
.state('dashboard',{
url:"/dashboard",
abstract: true,
templateUrl:'templates/dashboard.html',
controller:"myCtrl"
})
.state('courses',{
url:'/courses',
templateUrl:'templates/courses.html',
controller:'myCtrl'
})
.state('transactions',{
url:'/transactions',
templateUrl:'templates/transactions.html'
})
.state('oops',{
url:'/oops',
templateUrl:'templates/oops.html',
controller:'myCtrl'
})
}])*
view :
<ion-nav-view></ion-nav-view>
i found all my templates are getting loaded at once (on browser console testing) when i call my base route (also for any route,whichever i called for the first load of app).
where as when i use the ui-router for the nonionic app like :
myApp.config(function($stateProvider,$urlRouterProvider){
/*throw the rest url to home page*/
$urlRouterProvider.otherwise("/single");
$stateProvider
.state("single",{
url:"/single",
templateUrl:"templates/single.html"
})
.state("portfolio",{
url:'/portfolio',
templateUrl:'templates/portfolio.html',
controller:"myCtrl"
})
.state("nested",{
url:"/nested",
templateUrl:"templates/nested.html"
}
)
.state("nested.viwe1",{
url:"/view1",
templateUrl:"templates/nested.view1.html"
})
.state("nested.viwe2",{
url:"/view2",
templateUrl:"templates/nested.view2.html"
})
});
view:
<div ui-view class="my-element"></div>
only the demanded templates (configured with the route),is getting loaded.
So is the Ionic loads all template initially or else i am flowing wrong with the code.
Return the templateUrl by a function. Like this,
templateUrl: function() {return 'templates/courses.html';}
https://github.com/driftyco/ionic/issues/3819
http://ionicframework.com/docs/platform-customization/dynamic-templates.html

Why nested state's content gets appended to root state html?

root html:
<html> stuff <div ui-view></div> stuff </html>
partial template
<div class="article-view">
{{date_published}}
</div>
When I navigate using $state.go('root.' + entryStateUrl); the url changes but new state's content (template) is appended right after root's state content. I need to show template on new clean page. How to do it?
Maybe similar to this SO.
Edit
I generated and attached states like this:
$stateProviderRef.state('root.' + generatedStateName,
{ url: 'root/' + generatedStateName,
templateUrl : 'partials/article-view.html',
controller: function($scope){
$scope.date_published = data.date_published;
}
});
UPDATE
When I changed it like this it stopped working:
$stateProviderRef.state(generatedStateName,
{ url: generatedStateName,
templateUrl : 'partials/article-view.html',
controller: function($scope){
$scope.date_published = data.date_published;
}
});
Redirecting to a new page means you won't be able to bind to the controller used from the origin page, since the redirected page won't have any attachment to your js code.

Resources