I have an MVC 5 app with areas and I am trying to use the ui-router for AngularJs within one of my areas but I noticed that the templateUrl is wrong. It is trying to use a relative path but since I am using MVC routes and an Area the path to the template is incorrect.
The url to my area controller action is localhost:3789/Admin/UserManager .
The actual path is /Areas/Admin/Scripts/app/usermanager/partials/userlist.html .
angular.module("bsAdmin.userManager", ["ngResource", "ui.router", "ui.bootstrap", "bsPromiseTracker", "bsBusy", "angular-growl", "ngAnimate"])
.config(function ($stateProvider, $urlRouterProvider) {
// default state
$urlRouterProvider.otherwise("/userlist");
$stateProvider
.state('userlist', {
url: "/userlist",
templateUrl: "partials/userlist.html"
});
});
Angular ui-router tries to load the partial template using localhost:3789/Admin/partials/userlist.html
What are some techniques I can use so that the script will use the correct url to load the partial?
If your Angular javascript is in your .cshtml file, you can use the ASP.NET MVC URL helper to build the URL.
$stateProvider
.state('userlist', {
url: "/userlist",
templateUrl: "#Url.Content("~partials/userlist.html")"
});
});
Related
I'm working with an app on Angular 1 and Ui-router, and I am trying to make so that data in urls will be preserved between states. I have read about the queryParamsHandling:'preserve' feature on Angular 2.0. However I am currently stuck with Angular 1 and I need to resolve how to keep the url data the same between states.
One option I was considering was to preserve the url:params data between states was with the ui-sref, however so far unsuccessful.
Does anyone have good tips how to resolve this?
Thanks
Router File --> route.js:
angular
.module('moduleName')
.config(['$stateProvider', stateProvider])
function stateProvider($stateProvider) {
$stateProvider
.state('lessonDetails', {
url: '/:lessonId/details',
views: {
'content': {
templateUrl: 'lesson/lesson-details.html',
controller: 'lessonController'
}
}
});
}
HTML file --> lesson-details.html:
<a ui-sref="lessonDetails({'lessonId': 123})">Go to details</a>
Controller -- > lesson-details.js
angular
.module('moduleName')
.controller('lessonController', ['$scope', '$stateParams', lessonController]);
function lessonController($scope, $stateParams){
//use lessonId passed as params using $stateParams
console.log($stateParams.lessonId)
}
I am moving my mvc application in angularjs.
I have different areas in the application. I want to move between areas using MVC routing. And I want to use Angular routing inside each area.
So in my server I have default routing configuration... and in my angular I have the following configuration:
.config(function ($routeProvider, $locationProvider) {
var viewBase = '/Views/AngularTemplates/';
$routeProvider
.when('/Servizi/Cpe/New', {
templateUrl: viewBase + 'Cpes/insert.html',
controller: 'cpesController'
})
.when('/Servizi/Cpe', {
templateUrl: viewBase + 'Cpes/list.html',
controller: 'cpesController'
});
$locationProvider.html5Mode(true);
})
Servizi is my area and Cpe is the controller in the area... When I move from /Servizi/Cpe to /Servizi/Cpe/New it works because it is managed by angular.
But I cannot move from /Home to /Servizi because it's managed by MVC routing.
How can do that ?
A very quick solution to move between 'areas' would be:
window.location.href = "myAreaPath/mypage.asp";
Try it and tell us if work for you ;)
I try to create an app with Laravel 5.3 and AngularJS. I want to use the routes and templates from Angular instead of Laravel.
Here is the web.php file from Laravel:
Route::get('/', function () {
return view('index');
});
And here is a part of the ui-router in AngularJS:
routeConfig.$inject = ['$stateProvider'];
function routeConfig ($stateProvider) {
// Routes
$stateProvider
.state('home', {
url: '/',
templateUrl: 'app/views/home.html'
})
.state('register', {
url: '/register?oauth_token&oauth_verifier',
templateUrl: 'app/views/register.html',
controller: 'RegisterController',
controllerAs: 'registerCtrl'
})
};
I have also enabled the html5mode and the base url on head. The problem now:
When I am at home and click the link to go on register page, it works. But If I try to load directly the register page, it loads it through laravel routes and since I haven't mentioned anything about it there, I have a NotFoundHttpException.
That's because when you refresh all routes are handled by laravel first.
I had the same problem and there are 2 approaches:
either put the angular app on a different domain ... but you will run
into CORS issues
or tell laravel to route any route to angular app, and that's easier
Route::any('{path?}', function()
{
return view("index");
})->where("path", ".+");
Here's the scenario:
I have a main AngularJS app that uses ui-router for routing, like any other app. I also have a smaller AngularJS module that functions as its own app, not requiring that it be a submodule of my larger app.
I would like for the smaller app to handle its own routing and templating, too. Goal here being, the mini app can be loaded by another AngularJS app, or loaded on its own, and have all its routing and templates set up already.
Main App:
angular
.module('mainApp')
.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state('mainAppParent', {
url: '/',
templateUrl: 'main.html',
controller: 'mainCtrl',
controllerAs: 'vm'
})
.state('mainAppParent.miniJournalApp', {
url: '/journal'
});
}]);
Mini App:
angular
.module('miniJournalApp')
.config(['$stateProvider', function($stateProvider) {
$stateProvider
.state('mainJournalState', {
url: '/',
templateUrl: 'mainJournalView.html',
controller: 'JournalCtrl',
controllerAs: 'vm'
})
.state('newEntry', {
url: '/new'
});
}]);
I'm planning on having all the components of the mini app being an AngularJS .component(), so I'm wondering if the correct way to do this is to just let the parent app handle routing, and let the mini app handle the templates when I define each component/directive. Then, when I want to load the mini app on its own, I would just write a wrapper AngularJS module with new routing.
As we can see that in angular ui router we are using $stateProvider so basically we are using a provider.
In an angular app the provider is loaded once and then angular puts the instance in cache so next time when the provider is injected somewhere then same instance is used.
As same instance of $stateProvider is used across angular app so it is perfectly fine to define separate states for submodules.
And this is a good practice for code maintenance.
I have used it in many projects
How do I configure sailjs to render angular templates served by angular controllers? (i.e., these are templates served from the assets directory, not the view directory where Jade server-side templates normally reside.)
I succeed at rendering views/layout.jade at '/' by setting my views engine to Jade and route to
module.exports.routes={
'/': {
view: 'layout'
}
}
I put my angular templates in assets/templates but Express cannot find them. My ui-router config below tries to serve helloWorld.html but the file is not found (because it is actually still helloWorld.jade);
app.config(function ($stateProvider, $locationProvider) {
$stateProvider
.state('home', {
resolve: {},
url: '/',
templateUrl: '/views/helloWorld.html',
controller: 'HelloWorldCtrl',
controllerAs: 'HelloWorld'
})
});