Here is my code:
var myApp = angular.module('myApp', [
'ngRoute',
'artistControllers'
]);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/list', {
templateUrl: 'includes/list.html',
controller: 'artistListController',
controllerAs: 'alc',
}).
when('/detail/:id', {
templateUrl: 'includes/detail.html',
controller: 'artistDetailController',
controllerAs: 'adc',
}).
otherwise({
redirectTo: '/list'
});
}]);
My index.html
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular/angular-route.min.js"></script>
<script src="lib/angular/angular-animate.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script> //include artistListController and artistDetailController
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="main" ng-view></div>
</body>
</html>
If I use angular-route.min.js v1.4.3, it will throw $injector:unpr Unknown Provider and the url is stuck on http://127.0.0.1:56116/index.html
angular-route.min.js v1.2.10 works fine and the url is http://127.0.0.1:56116/index.html/#list
Anybody can help me with this? Thank you
This error results from the $injector being unable to resolve a required dependency. To fix this, make sure the dependency is defined and spelled correctly.
Removing artistControllers from your angular.module will solve the problem.Also include artistListController and artistDetailController in your index.html file.
including all the controller in your index.html like this:
<body>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers/artistListController.js"></script>
<script type="text/javascript" src="js/controllers/artistDetailController.js"></script>
</body>
In your app.js
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/list', {
templateUrl: 'includes/list.html',
controller: 'artistListController'
}).
when('/detail/:id', {
templateUrl: 'includes/detail.html',
controller: 'artistDetailController'
}).
otherwise({
redirectTo: '/list'
});
}]);
Related
I have been trying to make a angularjs application where I want to route to a different html page on a button click. But is not working for some unknown reasons.
My html code
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angular.js#*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<script data-require="angular.js#*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular-route.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="Controller">
<h1>Plunkr Example</h1>
<button ng-click="changeview()">ClickMe</button>
<h1>MainPage</h1>
</body>
</html>
My Code
var app = angular.module("app", ['ngRoute'])
app.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/Details', {
templateUrl: 'details.html'
}).
when('/Main', {
templateUrl: 'main.html'
}).
otherwise({
redirectTo: '/Main'
});
}]);
app.controller("Controller", function($scope,$routeParams,$location){
//$scope.ProductId = $routeParams.id;
$scope.changeview = function () {
console.log('in function changeview');
$location.path('/Details');
}
})
Here is my plunkr
Please help.
You have missed to add the ng-view directive. It needs to be included for the routing to be able to rendered the templates
<div ng-view></div>
Working plunker
You need to have ng-view directive in index.html
<div class="viewWrapper">
<div ng-view></div>
</div>
WORKING DEMO
Im uning angular to make page login, I use template login.html and controller login.html, the problem is when I call login controller the main controller will be executed automatically !
and I found : warning: tried to load angular more than once.
this is my JS:
trainingApp.config(['$routeProvider' ,function($routeProvider){
$routeProvider
.when('/login',{
templateUrl: 'login.html',
controller: 'loginCtrl'
})
.when('/',{
templateUrl: 'index.html',
controller: 'mainCtrl'
})
.otherwise({
redirectTo : "/"
});
}]);
trainingApp.controller('loginCtrl',['$scope', function($scope){
$scope.alertt = function(){
// console.log("ok");
alert("okkkkkkk");
}
}]);
trainingApp.controller('mainCtrl', ['$scope','$timeout', 'apiService','$location', function ($scope,$timeout, apiService,$location) {
console.log('hello');
}]);
index.html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Plannification Des Formations</title>
<link rel="stylesheet" type="text/css" href="mystyle/bootstrap-tokenfield.css">
<link rel="stylesheet" type="text/css" href="mystyle/bootstrap.min.css">
<link rel="stylesheet" href="mystyle/bootstrap-datepicker.css">
<link rel="stylesheet" href="mystyle/jquery-ui.css">
<script src="public/jquery-1.11.2.min.js"></script>
<script src="public/jquery-ui.js"></script>
<script src="public/bootstrap-datepicker.js"></script>
<script src="node_modules/lodash/lodash.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/moment/min/moment-with-locales.min.js"></script>
<script src="public/daypilot/daypilot-all.min.js"></script>
<script src="public/bootstrap-tokenfield.js"></script>
<script src="public/bootstrap.min.js"></script>
<!-- <script src="bower_components/angular-route/angular-route.js"></script> -->
<script src="controllers/app.js"></script>
<script src="controllers/mainCrtl.js"></script>
<script src="services/apiService.js"></script>
</head>
<body data-ng-app="training" data-ng-controller="mainCtrl" ng-view>
.......
</body>
</html>
login.html
<button id="btn-login" class="btn btn-success " ng-href="#!/">Login </button>
Please remove ng-controller="mainCtrl" from your html, as you have already defined controller in $routeProvider config.
Please change your code like this.
.when('/',{
templateUrl: 'index.html',
controller: 'mainCtrl',
abstract: true,
})
I am getting error in routing to about page. Home page is loading successfully. I am new to angular. Please help me to do it successfully.
here is the code link
enter code here Plunker Link
find code here
index.html
<!DOCTYPE html>
<head>
<title>Title</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-bootstrap/0.5pre/assets/css/bootstrap.min.css">
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/main.css">
</head>
<body ng-app="demo">
<div ng-view></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
Js
// Code goes here
var app = angular.module("demo", ['ngRoute']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/home.html',
controller: 'homeController'
}).
when('/about', {
templateUrl: 'partials/about.html',
controller: 'aboutController'
}).
otherwise({
redirectTo: 'partials/404.html'
});
}
]);
app.controller('mainController', function() {});
app.controller('homeController', function($scope, $http, $location){
$scope.login= function() {
$location.path("/home");
}
});
app.controller('aboutController', function($scope, $http) {
$scope.about= function() {
$location.path("/about");
}
});
Correct your references as <script src="main.js"></script> and create partial/home.html. It will work then.
Try to utilize browser developer tool (F12), it will you help to find actual error.
There are several issues with your plunker.
You don't have the partials created in your plunker version. You need to create the files partials/about.html and partials/home.html.
The css/main.css. You have created a style.css in the root folder, but referred css/main.css in index.html.
3.The path to main.js was wrong. The file is in the root folder and you referred it as js/main.js. Please refer it as <script src="main.js"></script>
You have to call the routes from the partials.
You have created the about method in aboutController. But, it should be in the home controller. About controller will only be instantiated when you route to the about.html page.
You have referred an URL '/home' which is not defined in the main.js.
Please find the following working plunker.
https://plnkr.co/edit/APKWvpuTfivua5CovRp7?p=preview
// Code goes here
var app = angular.module("demo", ['ngRoute']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'home.html',
controller: 'homeController'
}).
when('/about', {
templateUrl: 'about.html',
controller: 'aboutController'
}).
when('/login', {
templateUrl: 'login.html',
controller: 'loginController'
}).
otherwise({
redirectTo: 'partials/404.html'
});
}
]);
app.controller('homeController', function($scope, $http, $location){
$scope.login= function() {
$location.path("/login");
}
$scope.about= function() {
$location.path("/about");
}
});
app.controller('aboutController', function($scope, $http) {
$scope.backToHome = function() {
window.history.back();
}
});
app.controller('loginController', function($scope, $http) {
$scope.backToHome = function() {
window.history.back();
}
});
index.html:
<!DOCTYPE html>
<head>
<title>Title</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-bootstrap/0.5pre/assets/css/bootstrap.min.css">
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body ng-app="demo">
<div ng-view></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>
<script src="main.js"></script>
</body>
</html>
i'm new to angularjs,my problem is that i cannot render the views by using routes,here is my folder structure,i can't figure what is the problem with my code
index.html
three.html
two.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>AngularJS Routes example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
</head>
<body ng-app="sampleApp">
Route 2<br/>
Route 3<br/>
<div ng-view></div>
<script>
var app = angular.module("sampleApp", ['ngRoute']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/three', {
templateUrl: 'three.html',
controller: 'RouteController'
}).
when('/two', {
templateUrl: 'two.html',
controller: 'RouteController'
}).
otherwise({
redirectTo: '/'
});
}]);
app.controller("RouteController", function($scope) {
})
</script>
</body>
I have written this small sample for ngRoute
http://plnkr.co/edit/bJOai9XVGJcsA9zOriUN?p=preview
But it will keep failing with TypeError: undefined is not a function.
I have searched the web and the suggestions are not working for me. any ideas why it does not like ngRoute?
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular-route.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="Foo">
<h1>Hello Plunker!</h1>
<div ng-view></div>
</body>
</html>
script.js
var app = angular.module('Foo', ['ngRoute']);
app.configure(function($routeProvider){
$routeProvider
.when('/', {
templateUrl: 'foo.html',
controller: 'FooCtrl'
})
.otherwise({
redirectTo: '/'
});
});
app.controller('FooCtrl', function($scope){
$scope.message = "Hello World";
});
The name of the function is config(), not configure(). See https://docs.angularjs.org/api/ng/type/angular.Module
Also, you shouldn't use ngRoute 1.2.20 with angular 1.2.23. Make sure the versions are identical.