angular.js - angularjs怎么实现跳转的,是要写路由吗
黄舟
黄舟 2017-05-15 16:59:59
0
3
505

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
过去多啦不再A梦

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

左手右手慢动作

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.

Ty80

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!