【问题标题】:ui-sref not generating working hrefui-sref 不生成工作href
【发布时间】:2016-03-16 05:04:25
【问题描述】:

所以我是 Angular 的新手,我整天都在努力让 ngRoute 工作但没有任何成功。所以我决定尝试使用 ui-router,但这对我也不起作用。 (angularjs 1.5.0)

index.html

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
    <title>My app</title>
    <script src="js/libs/angular.min.js"></script>
    <script src="js/libs/angular-ui-router.min.js"></script>
    <script src="js/main.js"></script>
</head>
<body>
    <div ui-view></div>
    <a ui-sref="state1">State hello</a>
    <a ui-sref="state2">State show</a>
</body>
</html>

main.js

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider)
{
    $urlRouterProvider.otherwise("/state1");

  $stateProvider
    .state('state1', {
      url: "/state1",
      templateUrl: "partials/hello.html"
    })
    .state('state2', {
      url: "/state2",
      templateUrl: "partials/show.html"
    });
});

hello.html & show.html

<div>{{"hello"}}</div>

加载 index.html 时出现此错误:

angular.min.js:6 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=MyApp&p1=Error%3A%2…

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    您使用了错误的模块名称。

    <html ng-app="MyApp">
    
    var myApp = angular.module('myApp', ['ui.router']);
    

    在您的 js 文件中将您的模块名称更改为“MyApp”。即

    var myApp = angular.module('MyApp', ['ui.router']);
    

    【讨论】:

    • 要搭载这一点,最好使用一种命名约定并坚持下去。我在我的项目中使用 camelCase。
    • 知道你整天都在努力解决的问题太简单了,这很糟糕。我一直在学习新东西。大声笑我也使用 camelCase :) @Mangs 你能把它标记为接受吗?
    • 我实际上在使用 ngRoute 时遇到了困难,但是当这也引起了问题时,我不得不去这里。是的,我会的,但 SO 不允许再过 6 分钟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多