I have just begun trying to integrate ngRoute into an application I am building, but am having trouble getting ngRoute to work.
I have created a simple app to describe my attempt.
index.html:
<!DOCTYPE html>
<html ng-app="tester">
<head>
<meta charset="utf-8" />
<title>ngRoute Test</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.0/angular-route.js"></script>
<script src="app.js"></script>
</head>
<p>Here is a link to switch views:</p>
Test
<div ng-view></div>
</html>
app.js
var app = angular.module('tester', ['ngRoute']).config(function($routeProvider) {
$routeProvider
.when('/tests', {
templateUrl: 'tests.html',
controller: 'TestCtrl'
});
});
app.controller('TestCtrl', ['$scope', function($scope){
$scope.title = "This is a Test Page";
}]);
tests.html
<p>{{ title }}</p>
Here is a plunkr that shows my attempt: http://plnkr.co/edit/ntgV5xFTl46tBugEnCVe?p=preview. Any help on this issue would be much appreciated!
Since you don't have the Html5 Mode enabled you need to make the link to the view like this:
Test
Check this plunker: http://plnkr.co/edit/Pn4rzbFSxcjyfeojJyF1?p=preview
Related
I'm a beginner to AngularJs and currently working on a small test project. I found out the app perfectly working fine on Firefox but not in IE 11 and Chrome. I've been trying most of the fixes in the internet. adding meta tags and all but none of them worked for me. Here's my code.
Code
'use strict';
var app = angular.module("myApp", ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
});
$routeProvider.when('/services', {
templateUrl: 'services.html',
controller: 'ServicesCtrl'
});
$routeProvider.when('/clients', {
templateUrl: 'clients.html',
controller: 'ClientsCtrl'
});
$routeProvider.otherwise({redirecTo: '/'});
}]);
app.controller('HomeCtrl', ['$scope', function($scope){
$scope.message = 'Welcome to Inspire';
}]);
app.controller('ServicesCtrl', ['$scope', function($scope) {
$scope.message = 'Everyone come and see how good I look!';
}]);
app.controller('ClientsCtrl', ['$scope', function($scope) {
$scope.message = 'These are clients';
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<ul>
<li>
Home
</li>
<li>
Services
</li>
<li>
Clients
</li>
</ul>
<div ng-view=""></div>
</body>
</html>
I'd highly recommend you run through
Codeacademy.com
and
Udacity's front-end nano-degree(it's free if you scroll down and start on supporting courses).
https://www.udacity.com/course/front-end-web-developer-nanodegree--nd001
I see a lot of bad practice in the code snippets above and I don't recommend you start learning a framework until you feel comfortable with HTML/CSS and JavaScript.Once you do that you should then try picking up Angular.
As for your answer
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css"/>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<ul>
<li>
Home
</li>
<li>
Services
</li>
<li>
Clients
</li>
</ul>
<div ng-view=""></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
</body>
</html>
You'll also have to install ng-Route https://docs.angularjs.org/api/ngRoute. Check this out for details. Again please don't continue to learn Angular. You NEED to understand JavaScript and best practices before you continue with a framework.
Go through those two places I recommended and you should be okay.
I'm creating a website and I'm using JavaScript + AngularJS, presently I create the routes for the application, but it always returns 404 not found on the server, I've tried several examples of the internet but none of them worked, here's an example of my code, remembering that The html pages are inside the WebContent:
<html ng-app="angularRoutingApp">
<head>
<title>Home</title>
<meta charset="utf-8">
</head>
<body>
<div>
index <br> home
</div>
<div ng-view></div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.3/angular-route.js"></script>
<script>
var angularRoutingApp = angular.module('angularRoutingApp', ['ngRoute']);
angularRoutingApp.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/index", {
template: "/index.html"
})
.otherwise({
redirectTo: '/home.html'
});
});
</script>
</body>
</html>
you forgot the ng-view directive.
also,
the href need to be with a hash prefix - or using the Link directive.
the redirectTo key need to provide a view [name/address] & not a
template
if you want to use template files & not inline code use the templateUrl key
you can see an example of your code here: http://codepen.io/AceDesigns/pen/ZLXLKa
<html ng-app="angularRoutingApp">
<head>
<title>Home</title>
<meta charset="utf-8">
</head>
<body>
<div>
index<br>
home
</div>
<div class="">
<ng-view></ng-view>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.3/angular-route.js"></script>
<script>
var angularRoutingApp = angular.module('angularRoutingApp', ['ngRoute']);
angularRoutingApp.config(function($routeProvider){
$routeProvider
.when("/index", {template:"<div>INDEX VIEW</div>"})
.when("/home", {template:"<div>HOME VIEW</div>"})
.when("/tempFile", {templateUrl: "views/temp_file.html"})
.otherwise({redirectTo: '/home'});
});
</script>
</body>
</html>
I want to dynamically add menu JavaScript file and html to content.html, but it can't do.
I created simple example
demo
I try move "<script src="menu.js"></script>" to menu.html
Your code moves away from the whole idea of writing single page app with angular. I have updated it to give you basic idea of how you would do the routes and share templates and use controller.
Check out the plunkr
html
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="menu.css" />
<script data-require="angular.js#1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
<script data-require="ui-router#*" data-semver="0.2.15" src="https://cdn.rawgit.com/angular-ui/ui-router/805e69bae319e922e4d3265b7ef565058aaff850/release/angular-ui-router.js"></script>
<script src="menu.js"></script>
<script src="index.js"></script>
</head>
<body>
<div id="menu" ng-include="'menu.html'"></div>
<div ui-view></div>
</body>
</html>
js
angular.element(document).ready(function() {
angular.bootstrap(document, ["app"]);
});
angular.module('app',['moduleContent', 'moduleMenu', 'ui.router']);
var app = angular.module('app');
app.config(function($stateProvider) {
$stateProvider
.state('index', {
url: "",
templateUrl: "first.html",
controller: 'firstCtrl'
})
.state('second', {
url: "/second",
templateUrl: "second.html"
})
});
app.controller('firstCtrl', ['$scope', function($scope) {
$scope.page = "first";
}]);
//Conttent module
angular.module('moduleContent',[])
.controller('contentCtrl', contentCtrl);
function contentCtrl(shareData)
{
shareData.currentPage = 0;
}
I couldn't get my angular code to run in plunker. I have attached the details. Could any of you help me out? Basically it's a problem with ngcontroller I guess but I am not sure.
<!DOCTYPE html>
<html ng-app>
<head>
<script data-require="angular.js#*" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="MainController">
{{ 5 / 2 }}<br>
{{message}}
</body>
</html>
Contents of javascript script.js file
var MainController = function($scope){
$scope.message = "Welcome!";
};
Plunk
http://plnkr.co/edit/mzgdELALCP7DN2ikJHsC?p=preview
In version 1.3.*, you cannot longer declare a global controller function.
Instead define a module, and use your controller function:
var SidController = function($scope){
$scope.message = "WElcome.";
};
SidController.$inject = ['$scope'];
angular.module('app', []).controller('SidController', SidController);
In your html
<html ng-app="app">
See this plunker.
I had the same issue.. Well done on starting a tutorial! Simply replace the script in your index.html with
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
I suppose the library Plunker currently has is outdated or something.
Working Plunkr (with a different AngularJS version. #user3906922 has a better answer, where your version stays the same).
Use this for HTML for instance:
<!doctype html>
<html ng-app>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="SidController">
<h1>Hello Plunker!</h1>
{{ 5 / 2 }}<br>
{{message}}
</div>
</body>
</html>
Check this plunkr
You need to define your module and then register the controller
http://plnkr.co/edit/9zAV5nSWH05FYscEWYZ5?p=preview
angular.module( 'demoApp', []);
angular.module( 'demoApp' )
.controller( 'SidController', function($scope){
$scope.message = "WElcome.";
});
You should create your module.
angular.module('app', []).controller("SidController", function($scope) {
$scope.message = "WElcome.";
});
Then in your html element, set your attribute like this:
ng-app="app"
I am using AngularJS and ui-router. My code has ng-controller and ui-view on the same element and the controller doesn't get triggered
http://plnkr.co/edit/UphvqV01R7m0WwlY67YA?p=preview
HTML:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.2.7" src="http://code.angularjs.org/1.2.7/angular.js" data-require="angular.js#1.2.x"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.0/angular-ui-router.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ui-view="main" ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
</div>
</body>
</html>
Javascript:
var app = angular.module('plunker', []);
app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider
.state("home", {
url: "^",
resolve: {
test: function() {
alert("Triggered resolve home");
return true;
}
}
})
}]);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
Can you help to fix it or explain why?
You need to have your main module ultimately dependent on 'ui.router' somehow. Right now you have it dependent on nothing ([]).
var app = angular.module('plunker', ['ui.router']);
Don't do this.
When you transition to a state, ui.router assigns a controller to each active uiView element, so any controller you had previously assigned would be replaced.
Just wrap the uiView in a parent element and apply the controller to that.
Also, as Words Like Jared has pointed out, you're not even importing the ui.router module, so you need to do that, too.