我的ng include不起作用

my ng-include does not work

本文关键字:不起作用 include ng 我的      更新时间:2023-10-05

我是AngularJS的新手。我正在尝试使用ng-include,但它没有显示我引用的html文件。

app.js代码

    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="angularjs@1.5.0" 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文件。它们被称为testing1.htmltesting2.html.我甚至试图像ng-include中的"'testing1.html"那样直接引用它们。

但没有成功。有什么帮助吗?

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';
        }
    }]);

Plunker供您细读。

app.controller('mainCtrl', [$scope, function($scope)...你的[$scope必须像字符串一样是['$scope',改变它就会好起来)