当我在各种浏览器中或在使用节点的localhost上运行时,我无法在我的角度应用程序中以ng-view呈现模板。控制台没有抛出任何错误。我已经阅读了我在网上可以找到关于这个问题的所有内容,但没有一个是我的问题的答案。我正在使用El Capitan OS的macbook pro。我想知道在过去的一年里,作为一名初学者编写者,我是否通过sudo安装东西并在没有虚拟环境的情况下运行东西,对我的计算机做了一些有趣的事情。或者也许这里有一些愚蠢明显的错误,我在尝试每一个我能想到的排列时都忽略了。
这是我的index.html文件:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body ng-app="OIApp">
<nav id="mainnav" class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<h1>My App</h1>
</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="active">
<a href="#/">Home</a>
</li>
<li>
<a href="#/about">About</a>
</li>
<li>
<a href="#/blog">Blog</a>
</li>
<li>
<a href="#/products">Products</a>
</li>
</ul>
</div>
</nav>
<div ng-controller="OIController1">
<div ng-view>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-route.min.js"></script>
<script src="OIController.js"></script>
<script src="OIApp.js"></script>
<script src="config.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
App.js看起来像这样:
var app = angular.module("OIApp", ["ngRoute", "myControllers"])
Controller.js是这样的:
angular.module('myControllers', [])
.controller('OIController1', function($scope, $route) {
$scope.names = [
{name:"Colin Wilson", blurb: "Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"Graham Hancock", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"John Moriarty", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also ..."},
{name:"William Thompson", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."}
];
});
Config.js:
angular.module('myRoutes', ['ngRoute', 'myControllers'])
.config('$routeProvider', function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "/index.html",
controller : "OIController1",
})
.when("/about", {
templateUrl : "/about.html",
controller : "OIController1"
})
.otherwise({
redirectTo:'/'
});
});
这是about.html我正试图在ngView中呈现:
<div class="col-sm-2" ng-repeat="x in names">
{{x.name}}
{{x.blurb}}
</div>
答案 0 :(得分:1)
几乎没有问题:
//------------------------------------
//Let say, it's a app.router.js
//------------------------------------
angular.module('myRoutes', ['ngRoute', 'myControllers'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "index.html",
controller : "OIController1",
})
.when("/about", {
templateUrl : "about.html",
controller : "OIController1"
})
.otherwise({
redirectTo:'/'
});
}]);
//------------------------------------
//Let say, it's a app.module.js
//------------------------------------
angular.module("OIApp", ["ngRoute", "myRoutes", "myControllers"]);
//------------------------------------
//Let say, it's a app.controller.js
//------------------------------------
angular.module('myControllers', [])
.controller('OIController1', function($scope) {
$scope.names = [
{name:"Colin Wilson", blurb: "Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"Graham Hancock", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"John Moriarty", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also ..."},
{name:"William Thompson", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."}
];
});
我希望这应该有用
答案 1 :(得分:1)
您遇到语法错误,[]
中缺少route provider
。
<强> Pls run the below snippet.
强>
// Code goes here
var app = angular.module("OIApp", ["ngRoute", "myControllers"]);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when("/", {
})
.when("/about", {
templateUrl : "about.html",
controller : "OIController1"
})
.otherwise({
redirectTo:'/'
});
}]);
var test = angular.module('myControllers', [])
test.controller('OIController1', function($scope, $route) {
$scope.names = [
{name:"Colin Wilson", blurb: "Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"Graham Hancock", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."},
{name:"John Moriarty", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also ..."},
{name:"William Thompson", blurb:"Commodities are usually raw materials such as metals and agricultural products, but a commodity can also..."}
];
});
&#13;
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body ng-app="OIApp">
<script type="text/ng-template" id="about.html">
<div class="col-sm-2" ng-repeat="x in names">
{{x.name}}
{{x.blurb}}
</div>
</script>
<script>
</script>
<nav id="mainnav" class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<h1>My App</h1>
</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="active">
<a href="#/">Home</a>
</li>
<li>
<a href="#/about">About</a>
</li>
<li>
<a href="#/blog">Blog</a>
</li>
<li>
<a href="#/products">Products</a>
</li>
</ul>
</div>
</nav>
<div ng-controller="OIController1">
<div ng-view>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-route.min.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</body>
</html>
&#13;