可以't设法运行一个简单的angular js示例

Can't manage to run a simple angular js example

本文关键字:一个 简单 示例 js angular 运行 可以      更新时间:2023-09-26

我写了一个非常简单的angular js示例,但它不起作用,我不明白为什么。

如果有人看到我做错了什么,请告诉我,如果没有,我应该采取什么措施来面对它?

这是我的index.html:

<html>
<head>
<script src="/assets/libs/angular-1.4.7.js"></script>
<script src="/assets/libs/angular-ui-router.js"></script>
<script src="app.module.js"></script>
</head>
<body ng-app="example">
This is the index level
<br>
<div ui-view></div>
</body>
</html>

这是我唯一的视图(profile.html):

这是一个配置文件页面

这是js文件:

var example = angular.module("example", ['ui.router']);
example.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state("profile", {
            url: "profile",
            templateUrl: "templates/profile"
        })
    $urlRouterProvider.otherwise("/profile")
});

我预计结果是:

This is the index level
This is a profile page

但这就是我得到的:

这是索引级别的

所有的js文件路径都是正确的,因为我可以在开发工具中看到它们。

任何帮助都将不胜感激!

我认为这对您不起作用,因为templateUrl不正确。它应该是一个文件的正确路径和文件扩展名(除非你在后端使用了一些东西,或者模板是用templates/profile编译和命名的),但angular会调用XHR来获取该文件

由于.otherwise重定向到/profile,您应该将状态定义的URL更改为以下内容,以便ui-router将使用正确的模板加载正确的状态。

$stateProvider
  .state("profile", {
     url: "/profile",
     templateUrl: "templates/profile" //make sure this template has correct path
})