Angularjs routing - angularjs

I am learning Angularjs and having problem with routing. I write up a very simple slim demo example which uses out of the box MVC5. The home page display fine but when I click on the About link it doesn't show the About page and AboutController break point is not hit. And got the following error in Console
Error: [$compile:tpload] Failed to load template: templates/indexView.html (HTTP status: 404 Not Found).
If I swap the two route, then my About page is displayed but got the error when clicking Home link so I am sure the html can be loaded. What do I need to do in order to get the routing to work?
Below is the index.cshtml
#{
ViewBag.Title = "Home Page";
ViewBag.InitModule = "homeIndex";
}
#section Scripts {
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/js/home-index.js"></script>
}
<div data-ng-view=""></div>
Below is the About.cshtml
#{
ViewBag.Title = "About Page";
ViewBag.InitModule = "homeIndex";
}
#section Scripts {
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/js/home-index.js"></script>
}
<div data-ng-view=""></div>
below is the home-index.js
// home-index.js
var module = angular.module("homeIndex", ["ngRoute"]);
var angularFormsApp = angular.module('homeIndex', ["ngRoute"]);
angularFormsApp.config(["$routeProvider",
function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "templates/indexView.html",
controller: "HomeController"
})
.when("/About", {
templateUrl: "templates/aboutView.html",
controller: "AboutController"
});
}]);
angularFormsApp.controller("HomeController",
["$scope",
function ($scope) {
var x = 1;
}]);
angularFormsApp.controller("AboutController",
["$scope",
function ($scope) {
var x = 1;
}]);
The var x=1 has no meaning just for me to set a break point.
Below is indexView.html
<h3>Arrived at Index Page</h3>
aboutView.html
<h3>About Page</h3>
I also have the below in the _Layout.cshtml html tag to hook in angular
data-ng-app="#ViewBag.InitModule"

The problem seems to be that MVC is handling the routing that you expect to be handled by Angular.
Have a look at this question for some more information about having Both AngularJS and MVC in the same project:
Should I be using both AngularJS and ASP.NET MVC?
Here is a nice guide to getting started using the two together, it specifically touches on the routing as well:
Getting started with AngularJS and ASP.NET MVC
I hope that helps, please let us know if you need more information, or if something is not clear.

Related

Angular js controller says: "The controller with the name 'ctrEmployee' is not registered"

I'm new with angular JS MVC. First, I created on page ang separate angular JS file it was working fine then created another page added another controller in JS File and it started giving me error. then shifted my controllers to separate files. now on page is working and other is giving this error.
angular.js:15635 Error: [$controller:ctrlreg] The controller with the name 'ctrEmployee' is not registered.
I know there's already many questions related this error and tried all solutions but nothing worked. I'm exhausted and trying wasted almost a day on this error. Please don't mark my question duplicate may be I'm facing pother problem and have done any silly mistake. Here's my code:
App.js
var app;
(function () {
'use strict'; //Defines that JavaScript code should be executed in "strict mode"
app = angular.module("ngProject", ["ngRoute"]);
})();
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/Employee', {
templateUrl: 'Employee/Employee',
controller: 'Employee'
})
.when('/Company', {
templateUrl: '/Company/Company',
controller: 'Company'
}).otherwise({
templateUrl: "/Home/Index"
})
}]);
Company.js:
app.controller('ctrCompany', function ($scope, $http) { })
Employee.js:
app.controller('ctrEmployee', function ($scope, $http) { })
in _Layout.chtml head
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/Scripts/AngularScripts/App.js"></script>
in Views
<script src="~/Scripts/AngularScripts/Controller/Company.js"></script>
<h3>Companies</h3>
<div ng-app="ngProject">
<div ng-controller="ctrCompany" ng-init="GetAllData()" class="divList"> </div>
</div>
<script src="~/Scripts/AngularScripts/Controller/Employee.js"></script>
<div ng-app="ngProject">
<div ng-controller="ctrEmployee" ng-init="GetAllData()" class="divList"> </div>
</div>
Please help me out as I'm familiar with angular but not in .net MVC.
The JavaScript controller had an error in code and that's why it was showing error on controller name but error was confusing, after reviewing my code found error and after correction it worked. Thank you everyone.
Answer:
code had = instead of : in assigning value to parameter.

Unable to load angularJS view in .net core mvc

So I am having trouble loading my custom CreateBooking.html which only has this as a test
<div class="container">
<h2>AngularJS Test</h2></div>
My structure is like this
The main angular module
module Asda {
var app = angular.module("BookingApp", ["ngRoute"])
.config(function ($routeProvider: ng.route.IRouteProvider, $locationProvider: ng.ILocationProvider) {
$locationProvider.html5Mode(true);
});
}
The controller
module Asda {
export class CreateBookingController {
static $inject = ["$scope", "$http"];
bookingData: IBookingViewModel;
constructor($scope, $http) {
this.bookingData.Action = "Test";
}
static routing($routeProvider) {
$routeProvider.when("/createbooking",
{
controller: "CreateBookingController",
templateUrl: "/app/CreateBooking/CreateBooking.html",
controllerAs: "ctr"
});
}
}
angular.module("BookingApp")
.controller("CreateBookingController", CreateBookingController)
.config(["$routeProvider", CreateBookingController.routing]);
};
As I am using .net core MVC I am catching the request and loading a generic cshtml view which has this inside
<div ng-view>
So angular should be able to load its view... however the html in CreateBooking.html is not rendered..
I have added the files in this order in my head section
<script src="~/js/angular.min.js"></script>
<script src="~/js/angular-route.min.js"></script>
<!-- Main angular app module-->
<script src="~/App/BookingApp.js"></script>
<!-- Angular controllers-->
<script src="~/App/CreateBooking/CreateBookingController.js"></script>
angular and angular-route are both v1.7.9
I am at a loss.. :/
Dont forget to kickstart with ng-app!

AngularJs rotuer not working

I have a small AngularJS application that use the route feature to render ng-view section. For some reason, the controller and view are not rendering as expected.
This is my application:
angular.module('sitesApp', ['ngRoute']);
var app = angular.module('sitesApp');
app.config(['$routeProvider', function ($routeProvider)
{
$routeProvider.when('/members/sites', {
templateUrl: '/members/_sites',
controller: 'SitesCtrl'
});
}]);
var controllers = {};
controllers.SitesCtrl = function ($scope)
{
alert('SitesCtrl loaded');
}
app.controller(controllers);
My application entry point is "/members/sites". When the user enter this URL, I want that the router will present the file "/members/_sites" in my ng-view.
This is how "/members/sites" looks like:
<div class="container" ng-app="sitesApp">
<ng-view></ng-view>
</div>
<script src="~/Scripts/app/Members/sites-app.js"></script>
This is how "/members/_sites" looks like:
<h1>Hello world!</h1>
Expected: SitesCtrl will be loaded and "Hello world" title will be presented on the screen.
Actual: Nothing. SitesCtrl controller wasn't loaded (no alert executed) and nothing on the screen.
I using angularjs 1.4.9 (with angular-route.min.js).
What the problem of this code?

Using ng-route with Google-Apps-Script or some means of rendering a partial html view

I am trying to get ng-route working with a google-apps-script web app. I have managed to get basic angular.js functionality working with google-apps-script, but I can't seem to get ng-route to work. I have placed ng-view tags inside a page and have included a separate JavaScript page that contains the routeProvider function.
The ng-view never gets rendered and as far as I can make out the routeProvider does not get called.
Can anyone offer any advice on using ng-route with google-apps-script or suggest another way of rendering a partial html page with google-apps-script
Any answers greatly appreciated.
Have simplified my code and added below:
Code.gs
function doGet(e) {
var template = HtmlService.createTemplateFromFile('Index');
// Build and return HTML in IFRAME sandbox mode.
return template.evaluate()
.setTitle('Web App Window Title')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
index.html
<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<html>
<body ng-app="myApp">
<h1>NG View</h1>
<ng-view></ng-view>
<p>angular check {{'is' + 'working!'}}</p>
<? var url = getScriptUrl();?>
<p id="urlid"><?=url?></p>
</body>
</html>
<!-- Use a templated HTML printing scriptlet to import JavaScript. -->
<?!= HtmlService.createHtmlOutputFromFile('JavaScript').getContent(); ?>
Javascript.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://code.angularjs.org/1.3.15/angular-route.js"> </script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script>
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider){
console.log('routeProvider config');
var url = document.getElementById("urlid").innerHTML;
console.log('routeProvider config->' +url);
$routeProvider.when("/",
{
templateUrl: url+"?page=_app.html",
controller: "AppCtrl",
controllerAs: "app"
}
);
})
.controller('AppCtrl', function() {
var self = this;
self.message = "The app routing is working!";
});
</script>
_app.html
<div>
<h1>{{ app.message }}</h1>
</div>
When this runs angular check {{'is' + 'working!'}} works fine, but the ng-view does not get rendered the java console shows:
Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy.
The first obstacle is "sce"
$sce is a service that provides Strict Contextual Escaping services to AngularJS.
Refer link https://docs.angularjs.org/api/ng/service/$sce#trustAsResourceUrl
For the purpose of investigation, I disabled sce (this is not recommended, though)
$sceProvider.enabled(false);
Now the error is shifted to XMLHttpRequest cannot load... No 'Access-Control-Allow-Origin' header is present on the requested resource.
XHR requests to the Google Apps Script server are forbidden
Refer link https://developers.google.com/apps-script/guides/html/restrictions
Google Apps is delivering the files from a different origin than the scripts.google.com and angular js client code is not able to fetch the partial htmls from the same origin.
I guess approach of ng-view is not feasible given the restrictions placed by google apps.
Here is the final modified code
<script>
angular.module('myApp', ['ngRoute'])
.config(function($routeProvider,$sceProvider){
$sceProvider.enabled(false);
console.log('routeProvider config');
var url = document.getElementById("urlid").innerHTML;
console.log('routeProvider config->' +url);
$routeProvider.when("/",
{
templateUrl: url+"?page=_app.html",
controller: "AppCtrl",
controllerAs: "app"
}
);
})
.controller('AppCtrl', function() {
var self = this;
self.message = "The app routing is working!";
});
</script>
There has been some time since the question, but I'll post a reply either way.
If your partial html page is not too complicated and big, you can use template instead of templateUrl in the routeProvider, plus create a variable with the html you want to show. Something like this below:
var partial_page = "<span>partial page</span>"
$routeProvider.when("/",
{
template: partial_page,
controller: "AppCtrl",
controllerAs: "app"
}
It worked for me, but I wouldn't advise doing so for a complicated partial page as it may become difficult to read the code

Routing AngularJS not working

Hi I am working on the angular js 60ish min tutorial an am not able to figure out the routing functionality here is my code
Issue: When I open the html file nothing happens i do noy see any routing happening
view1.html and view2.html are in the same folder with main.html and angular.min.js file.
Please point the mistake in the code or how to debug this.
<!DOCTYPE HTML>
<HTML data-ng-app ="demoapp">
<head><title>New angular js app </title></head>
<body>
<div>
<div ng-view></div>
</div>
<script src = "angular.min.js"></script>
<script>
var demoapp = angular.module('demoapp',[]);
demoapp.config(function($routeProvider){
$routeProvider
.when('/view1',
{
controller:'SimpleController',
templateUrl:'view1.html'
})
.when('/view2',
{
controller:'SimpleController',
templateUrl:'view2.html'
})
.otherwise({ redirectTo:'/view1'});
});
demoapp.controller('SimpleController', function ($scope){
$scope.customers = [
{name:"ishan", city:"Delhi"},
{name:"Ankit", city:"New Delhi"},
{name:"subhash", city:"haridwar"},
];
$scope.addCustomer = function()
{
$scope.customers.push
({
name:$scope.newCustomer.name,
city:$scope.newCustomer.city
});
};
});
</script></body></html>
Since Angular 1.2 you have to include the route module.
var demoapp = angular.module('demoapp',['ngRoute']);
See the docs
Please open console on your browser to detect the errors. If any error is shown, you try to refer to the AngularJS Google CDN latest as follows:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
After this, if your code still isn't working fine, please turn on the Debug script on your browser to investigate more.
Please let me know if any problem still happens.
Hope this will help you!!
var demoapp = angular.module('demoapp',[ngRoute]);
don't forget Include the angular-route.js in your page

Resources