2016-07-13 2 views
0

Если я бегу простой AngularJS (пример W3Schools) приложение, использующее файл это один HTML работает правильно:AngularJS импорт выпуск

<!DOCTYPE html> 
<html> 

<head> 

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

</head> 

<body ng-app="myApp" ng-controller="mainController"> 

<div ng-view> 

</div> 

<p><a href="#/">Main</a></p> 

<a href="#banana">Banana</a> 
<a href="#tomato">Tomato</a> 

<p>Click on the links to change the content.</p> 

<p>The HTML shown in the ng-view directive are written in the template property of the $routeProvider.when method.</p> 

<script> 

var app = angular.module("myApp", ["ngRoute"]); 

app.config(function($routeProvider) { 
    $routeProvider 
    .when("/", { 
        template : "<h1>Main</h1><p>Click on the links to change this content</p>" 
    }) 
    .when("/banana", { 
        template : "<h1>Banana</h1><p>Bananas contain around 75% water.</p>" 
    }) 
    .when("/tomato", { 
        template : "<h1>Tomato</h1><p>Tomatoes contain around 95% water.</p>" 
    }); 
}); 

app.controller('mainController',function($scope){ 


}); 

</script> 

</body> 
</html> 

Когда я делю маршрутизацией от HTML-файла его больше не работает:

index.html

<!DOCTYPE html> 
<html> 

<head> 

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script> 

</head> 

<body ng-app="myApp" ng-controller="mainController"> 

<div ng-view> 

</div> 

<p><a href="#/">Main</a></p> 

<a href="#banana">Banana</a> 
<a href="#tomato">Tomato</a> 

<p>Click on the links to change the content.</p> 

<p>The HTML shown in the ng-view directive are written in the template property of the $routeProvider.when method.</p> 


<script src="Controller/routing.js"></script> 


</body> 
</html> 

Контроллер/routing.js

var app = angular.module("myApp", ["ngRoute"]); 

app.config(function($routeProvider) { 
    $routeProvider 
    .when("/", { 
        template : "<h1>Main</h1><p>Click on the links to change this content</p>" 
    }) 
    .when("/banana", { 
        template : "<h1>Banana</h1><p>Bananas contain around 75% water.</p>" 
    }) 
    .when("/tomato", { 
        template : "<h1>Tomato</h1><p>Tomatoes contain around 95% water.</p>" 
    }); 
}); 

app.controller('mainController',function($scope){ 


}); 

ответ

0

Неверный путь к файлу. Попробуйте:

<script src="/Controller/routing.js"></script>

Относительный путь должен начинаться с /.

+0

Не работает. – DistribuzioneGaussiana

0

Привет DistribuzioneGaussiana, Если вы проверяете вашу консоль, вы увидите браузер не может загрузить файл углового маршрута от КДС у вас есть provided.Add Http: источник КДС, сделать это - http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js и он будет работать.

+0

Не работает. – DistribuzioneGaussiana

+0

Вы добавили это - '' в вашем файле index.html? –

+0

Я пробовал. Но это не может быть ошибкой, потому что, когда я помещаю html и js-код (импортируя ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js) только в одном файле, мое простое приложение работает должным образом. – DistribuzioneGaussiana

Смежные вопросы