recherche

Maison  >  Questions et réponses  >  le corps du texte

javascript - angular-ui-router 路由跳转问题?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

<code><!DOCTYPE html>

<html lang="en" ng-app="testApp">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>

    <title>Title</title>

    <script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>

    <script type="text/javascript" src="bower_components/angular/angular.min.js"></script>

    <script type="text/javascript" src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>

</head>

<body>

<p ui-view></p>

<script type="text/javascript">

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

    testApp.config(function($stateProvider, $urlRouterProvider){

        $urlRouterProvider.otherwise('/');

 

        $stateProvider

                .state('index', {

                    url: '/',

                    template: '<ul><li ng-repeat="item in items" data-id="{{item.id}}">{{item.name}}</li></ul>',

                    controller: function($scope, $state){

                        $scope.items = [

                            {

                                name: '张三',

                                id: 1

                            },

                            {

                                name: '李四',

                                id: 2

                            },

                            {

                                name: '王五',

                                id: 3

                            }

                        ];

 

                        $(function(){

                            $('li').click(function(){

                                console.info($(this).data('id'));

                                var id = $(this).data('id');

                                $state.go('index.detail', {

                                    id: id

                                });

                            });

                        })

                    }

                })

                .state('index.detail', {

                    url: ':id',

                    template: '<h1>This is detail</h1>'

                })

    });

</script>

</body>

</html></code>

就是为什么我点击li的时候他不会跳转页面呢?地址栏是没错的啊?

PHPzPHPz2906 Il y a quelques jours276

répondre à tous(3)je répondrai

  • PHPz

    PHPz2017-04-10 17:02:23

    盗用了文档的代码,你为何不这样做呢?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    <code>$stateProvider

        .state('contacts', {

            abstract: true,

            url: '/contacts',

            templateUrl: 'contacts.html',

            controller: function($scope){

                $scope.contacts = [{ id:0, name: "Alice" }, { id:1, name: "Bob" }];

            }          

        })

        .state('contacts.list', {

            url: '/list',

            templateUrl: 'contacts.list.html'

        })

        .state('contacts.detail', {

            url: '/:id',

            templateUrl: 'contacts.detail.html',

            controller: function($scope, $stateParams){

              $scope.person = $scope.contacts[$stateParams.id];

            }

        })

         

    <!-- contacts.html -->

    <h1>Contacts Page</h1>

    <p ui-view></p>

     

    <!-- contacts.list.html -->

    <ul>

        <li ng-repeat="person in contacts">

            <a ng-href="#/contacts/{{person.id}}">{{person.name}}</a>

        </li>

    </ul>

     

    <!-- contacts.detail.html -->

    <h2>{{ person.name }}</h2></code>

    répondre
    0
  • 巴扎黑

    巴扎黑2017-04-10 17:02:23

    你检查下是不是页面和controller里面的问题。既然地址能跳那证明路由没有问题。你看看请求里面是否有加载你配置的那个页面。

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:02:23

    至少要在第二个state的url中加/, url:'/:id'

    répondre
    0
  • Annulerrépondre