search

Home  >  Q&A  >  body text

angular.js - angularjs怎么实现跳转的,是要写路由吗

想要实现点击链接会跳转到详情页面,我是初学angular还请大神们解答。谢谢

黄舟黄舟2744 days ago547

reply all(3)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:01:59

    Method 1: Use ngRoute that comes with angular
    Dependency: angular-route.js (bower install angular-route)
    Website: http://docs.angularjs.cn/api/ngRoute/service/$route#example
    Usage :
    a. You need to use the ng-view command in the ui to specify such as: <p ng-view></p> This is equivalent to the refresh area of ​​the page
    b. Configuration registration

    // 注入 ngRoute
    var angularApp = angular.module("Your App Name", ['ngRoute'])
    angularApp.config(function ($routeProvider) {
        $routeProvider.
        when('/list', {
            // 配置列表路由及 Controller
            templateUrl: 'partial/list.html', //TODO 列表页面
            controller: 'listController' //TODO 列表控制器
        }).
        when('/detail', {
            // 配置详情路由及 Controller
            templateUrl: 'partial/detail.html', //TODO 详情页面
            controller: 'detailController' //TODO 详情控制器
        }).
        otherwise({
            //默认路由
            redirectTo: '/list'
        });
    });

    Method 2: Use third-party ui-router
    Dependency: angular-ui-router.js (bower install angular-ui-router)
    Website: https://github.com/angular-ui/ui-router
    Usage:
    a. You need to specify the ui-view command in ui, such as: <p ui-view></p> This is equivalent to the refresh area of ​​the page
    b. Configuration registration

    // 注入 ui.router
    var angularApp = angular.module("Your App Name", ['ui.router'])
    angularApp.config(function ($stateProvider) {
        $stateProvider.
        state('list',{
            url:'/list',
            templateUrl: 'list.html',
            controller: 'listController'
        }).
        state('detail',{
            url:'/detail',
            templateUrl: 'detail.html',
            controller: 'detailController'
        })
    });

    You can refer to some articles for detailed usage and differences, each has its own advantages and disadvantages

    reply
    0
  • ringa_lee

    ringa_lee2017-05-15 17:01:59

    For single-page applications, you need to use $router to match the url and templatecontroller.

    <a href='#/detail/{{phone.id}}'>{{phone.name}}</a>
    app.config(function($routeProvider) {
        $routeProvider
            // route for the home page
            .when('/home', {
                templateUrl : '/static/view/layout/home.html',
                controller  : 'HomeController'
            })
            .when('/detail/:id', {
                templateUrl : '/static/view/detail.html',
                controller  : 'DetailController'
            })
    })
    
    app.controller("DetailController", function($scope, $routerParams){
        console.log($routerParams.id);//
    })

    If you don’t write a single-page application, it is a normal page address.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:01:59

    The above has been fully explained. The usual method is to use ui-router to jump to the state you defined, such as this.

    reply
    0
  • Cancelreply