I'm pretty new to Angular but loving it! I am trying to create a modal dialog to display a partial view. ui.bootstap.modal has an option which takes the URL to the partial view to be displayed. I have a route configured on my application module that looks like this:
angular.module('buggy').config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/lists', {
templateUrl: 'views/lists/list.html'
}).
when('/lists/create', {
templateUrl: 'views/lists/create.html'
}).
when('/lists/:listId', {
templateUrl: 'views/lists/partials/view.html'
}). //more stuff
I would like to show the partial template defined as when(/lists/:listId) from the above routes. So in my controller I'm attempting to open the modal dialog like so:
$scope.showList = function (list) {
$modal.open({
templateUrl:'lists/' + list._id,
scope:$scope
});
}
The modal dialog opens but the contents are just [object]. Do I need to define the route on the server side or can I use Angular routing to return the partial?
Thanks!
My understanding of the $routeProvider was flawed. I blame years of jQuery'n ;) I've got it working now. I believe the $routeProvider was returning an instance of the controller defined in my module configuration; not the actually template. I've changed my code like so:
$scope.showList = function (list) {
$scope.currentList = list;
$modal.open({
templateUrl: 'views/lists/modals/view.html',
backdrop: false,
scope: $scope,
controller: 'modalCtrl'
});
}
If this is not a good solution.. please comment. I have a lot to learn about Angular yet.
Thanks!
Related
There are a ton of examples of using the newer angular directives like ng-blur, ng-focus, form validation, etc. They all work great in a single page, or in plinkr, jsfiddle, etc. with the exception of the people who try to define the function on the global namespace, that mistake is WELL documented.
However, I was having a different problem.
I was using an example from Scotch.io. This one works great...until you introduce it into an SPA that is using angular-route :(
After many hours of fighting with the error 'Argument 'mainController' is not a function, got undefined', I found the answer in a comment from Hajder Rabiee.Thanks Hadjer, Love you man!
Hajder left this comment and in it, he says:
If you're using routes (high probability) and your config has a reference to a controller in a module that's not declared as dependency then initialisation might fail too.
E.g assuming you've configured ngRoute for your app, like
angular.module('yourModule',['ngRoute'])
.config(function($routeProvider, $httpProvider) { ... });
Be careful in the block that declares the routes,
.when('/resourcePath', {
templateUrl: 'resource.html',
controller: 'secondModuleController' //lives in secondModule
});
Declare secondModule as a dependency after 'ngRoute' should resolve the issue. I know I had this problem.
Even with this help it took me a minute to get it working, so I thought I would share my sample code here to help the next poor bastard that gets stuck on this.
First, in the place where i declare my routes:
var app = angular.module('sporkApp', ['ngRoute','validationApp']);
app.config(function ($routeProvider) {
$routeProvider
.when('/home',
{
controller: 'HomeController',
templateUrl: 'home/home.template.html'
})
.when('/tags',
{
controller: 'TagsController',
templateUrl: 'tags/tags.template.html'
})
.when('/test',
{
controller: 'mainController',
templateUrl: 'test/test.template.html'
})
.otherwise({ redirectTo: '/home' });
});
Then, you need to add your controller code somewhere, where it will get loaded in your shell page:
// create angular app
var validationApp = angular.module('validationApp', []);
// create angular controller
validationApp.controller('mainController', function($scope) {
// function to submit the form after all validation has occurred
$scope.submitForm = function() {
// check to make sure the form is completely valid
if ($scope.userForm.$valid) {
alert('our form is amazing');
}
};
});
Finally, you need to add the corresponding ng-app and ng-controller to some page element that wraps the controls you want to validate. I put the following inside of a div tag:
<div ng-app="validationApp" ng-controller="mainController">
I am developing an application that is supposed to be a single-page application.
The tools I am using are AngularJS, NodeJS, ExpressJS and Jade for templating.
So far I've been working with a page that has a ng-view directive on it, and I can change its content to display the page I want, while maintaining the side menu.
Now I've come to a point where I need to create a login/create account page (that I am calling 'intro', for now), and this one should use all the screen space, removing the menu as well.
How can I achieve this? My route file looks as follows:
var akaAcademicManagerApp = angular.module('akaAcademicManagerApp', ['ngRoute', 'akaAcademicControllers']);
akaAcademicManagerApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/dashboard', {
templateUrl: 'partials/dashboard',
controller: 'DashboardController'
}).
when('/profile', {
templateUrl: 'partials/profile',
controller: 'ProfileController'
}).
when('/intro', {
templateUrl: 'partials/introPage',
controller: 'IntroController'
}).
otherwise({
redirectTo: '/dashboard'
});
}]);
angular.module('akaAcademicControllers', []);
I think this can help you (yet, i don't think it's the optimal solution but it works)
in your index.html,after your body tag put this:
<div ng-include="accessToApplication()"></div>
Now in your controller:
$scope.loggedIn = false;// true when the user is logged in
$scope.accessToApplication = function () {
if (!$scope.loggedIn) {
return "partials/introPage.html";
}
else {
return "path to the page containing the ng-view";
}
};
I advice you to take a look at ui-router and multiple named views (https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views)
I have an issue which I will go on to explain. When using ControllerAS at the top of a html page such like this:
<div ng-controller="MyController as myControllerVM">
I can refer to things in my controller using the myControllerVM name.
Ie in my controller I set up a variable this and assign other variables to it like this:
var vm = this;
vm.number = 1;
I can then refer to this variable in the html like this:
myControllerVM.number
as long as at the top of my page I have
<div ng-controller="MyController as myControllerVM">
What I want to do is accomplish the same thing only through routing. I have tried this:
.when(myROUTE, {
templateUrl: 'template.html',
controller: 'MyController',
controllerAs: 'myControllerVM'
and assigned everything in the controller to the variable vm which points to this as before. But in my html page these values are not getting pulled through as they were when the "controller as" name as at the top of the html page, i.e. I can no longer refer to the values in the html as myControllerVM.name (the value isn't being pulled through). The reason I need it in the route is because I want to use different controllers for the one html page.
Why isn't using controllerAs in the route working.
I can't see all your code but you can find working example here
http://plnkr.co/edit/aedTeP2DbicGYwdGWKvU?p=preview
ie:
myApp.config(function($stateProvider, $urlRouterProvider) {
//
// For any unmatched url, redirect to /state1
$urlRouterProvider.otherwise("/state1");
//
// Now set up the states
$stateProvider
.state('state1', {
url: "/state1",
templateUrl: "state1.html",
controller: 'state1Ctrl',
controllerAs:'vm'
})
.state('state2', {
url: "/state2/",
templateUrl: "state2.html",
controller: 'state2Ctrl',
controllerAs:'vm'
});
});
myApp.controller('state2Ctrl', function(){
var vm = this;
vm.title= "That is my second"
});
myApp.controller('state1Ctrl', function($scope){
var vm = this;
vm.title= "fist controller";
});
You're doing everything correct with
.when(myROUTE, {
templateUrl: 'template.html',
controller: 'MyController',
controllerAs: 'myControllerVM'
I'm not sure if Angular documentation is clear enough on that, but the scope of router's current controller is ngView's scope. So you can use MyController scope variables (including the ones that use myControllerVM identifier) only in template or ngView element attributes. I guess it is the source of the problem.
I'm working on a AngularJS + OnsenUI project, and I'm having problems with the navigation.
Let's say that I have a module:
angular
.module('app.home', ['ui.utils','ngRoute','ngAnimate'])
.controller('HomeCtrl', HomeCtrl)
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'path/to/home/template',
controller: 'HomeCtrl'
})
.when('/test1', {
templateUrl: 'path/to/template',
controller: 'TestOneCtrl'
})
.when('/test2', {
templateUrl: 'path/to/template',
controller: 'TestTwoCtrl'
})
.otherwise({
redirectTo: 'path/to/home/template'
});
});
In the HomeCtrl I'm supposed to (depending on the result of certain functions) navigate to either test1.html or test2.html. My problem is that I don't know how to link the routeProvider to the the ons.navigator.pushPage function.
This doesn't work:
var url = '/test1';
$scope.navigator.pushPage( url, { animation : 'slide' } );
This works:
var url = '/absolute/path/to/template';
$scope.navigator.pushPage( url, { animation : 'slide' } );
My question is what do I need to do so I don't have to write the absolute path to the template in the url variable? Apparently I'm missing out on something, but I can't figure out what.
Thanks in advance!
I think it's because the path used in $routeProvider is not the same type of that of pageUrl used in navigator.pushPage().
$routeProvider.when(path, route);
and
navigator.pushPage(pageUrl, option);
Path is like the pattern or string of your app url found in the browser address bar. For example, "http://localhost:8000/app/index.html#/test1". That's when you can refer to this in the routeProvider as "/test1". However, in the navigator.pushPage(), you will need to specify exact url to the page just like how you set ur templateUrl inside $routeProvider. In other words, pageUrl = route.
That's just from my understanding though.
Using Angular I have a dozen or so routes setup similar to the following example code.
Is there a way to override which template and controller is loaded based on some other criteria while keeping the URL in tact? My goal is to display a login page when... lets say $scope.isLoggedIn = false. I don't want to change the URL to /login.
SomeApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/place', {
templateUrl: 'routes/place.html',
controller: 'PlaceCtrl'
})
.when('/test', {
templateUrl: 'routes/test.html',
controller: 'TestCtrl'
});
}]);
ngRoute is a very simple library that can basically only maps urls to controller/views. If you want more flexibility, try ui-router which has the ability to route based on state.
This isn't really doable with ngRoute, but with ui-router you can dynamically provide different templates based on just about anything you want.
$stateProvider.state('root',
url: '/'
controller: 'HomePageController'
templateProvider: [
'$rootScope'
'$templateCache'
'$http'
($rootScope, $templateCache, $http) ->
templateId = if $rootScope.isLoggedIn then "home-page-logged-in" else "home-page-not-logged-in"
templateId = "/templates/#{templateId}.html"
return $http.get(templateId, cache: $templateCache)
]
)
The catch is, as far as I know, you can't change the controller, only the template. Which kinda stinks.