angular ui路由器状态模板不工作

angular ui-router state template not working

本文关键字:工作 状态 ui 路由器 angular      更新时间:2023-09-26

我用ui路由器创建了一个基本的angular应用程序。我正在提供来自index.html的带有脚本标记的部分模板,但我得到了这个错误

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:1337/home.html

这是我的密码。

var app = angular.module('smApp', ['ui.router'])
    .config(['$stateProvider', function ($stateProvider) {
        $stateProvider
            .state('home', {
                url: '',
                templateUrl: 'home.html',
                controller: 'homeCtrl'
            })
    }])
    .controller('homeCtrl', ['$scope', function ($scope) {
        $scope.home = 1;
    }]);

index.html

<script type="text/script" id="home.html">
    <div>Home Page {{home}}</div>
</script>
<body>
    <div ui-view></div>
</body>

您需要更改包含模板代码的脚本标记的类型属性。对于Angular,为了识别模板,您需要将类型属性从"text/script"更改为"text/ng template"

你的index.html应该是

<script type="text/ng-template" id="home.html">
    <div>Home Page {{home}}</div>
</script>
<div ui-view></div>