Я новичок в AngularJS. Я пытаюсь использовать ng-include, но он не отображает html файлы, на которые я ссылался.
Код приложения.
var app = angular.module('myApp', []);
app.controller('mainCtrl', [$scope, function($scope)
{
$scope.templateID = 'testing1.html';
$scope.changeTemplateID = function($scope){
$scope.templateID = 'testing2.html';
}
}]);
Файл index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script data-require="[email protected]" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body ng-controller="mainCtrl">
<ng-include src="templateID"></ng-include>
<h1>Hello Plunker!</h1>
<p ng-click="changeTemplateID()">next</p>
</body>
</html>
В корневом каталоге есть два файла html. Они называются test1.html и testing2.html. Я даже пытался ссылаться на них напрямую, как "test1.html" в ng-include.
но никакого успеха. Любая помощь?
Две ошибки в вашем JS-коде:
var app = angular.module('myApp', []);
// vvvvvvvv must be a string
app.controller('mainCtrl', ['$scope', function($scope)
{
$scope.templateID = 'testing1.html';
// you don't need to pass $scope here:
$scope.changeTemplateID = function(){
$scope.templateID = 'testing2.html';
}
}]);
Плункер для вашего прочтения.
app.controller('mainCtrl', [$scope, function($scope)...
Ваша [$scope
должна быть ['$scope'
как строка, изменить ее, и все будет в порядке)