加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs ngRoute和ui.router对比

发布时间:2020-12-17 10:39:47 所属栏目:安全 来源:网络整理
导读:ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view ngRoute模块 $routeProvider服务 .when() .otherwise(); ui-router(angular-ui-router.js) ui-view ui.router模

ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比:

ng-router(angular-router.js) ng-view ngRoute模块 $routeProvider服务 .when() .otherwise();

ui-router(angular-ui-router.js) ui-view ui.router模块 $stateProvider服务 .state(),$urlRouterProvider服务 .otherwise();

ng-router:

    =>
  • >view1
  • >view2

<span style="color: #008000">//<span style="color: #008000">js
angular.module(<span style="color: #800000">'<span style="color: #800000">myApp<span style="color: #800000">',[<span style="color: #800000">'<span style="color: #800000">ngRoute<span style="color: #800000">'<span style="color: #000000">])
.config([<span style="color: #800000">'<span style="color: #800000">$locationProvider<span style="color: #800000">',<span style="color: #800000">'<span style="color: #800000">$routeProvider<span style="color: #800000">'<span style="color: #000000">,function($locationProvider,$routeProvider){
$locationProvider.html5Mode(<span style="color: #0000ff">false).hashPrefix(<span style="color: #800000">''<span style="color: #000000">);
$routeProvider
.when(<span style="color: #800000">'<span style="color: #800000">/view1<span style="color: #800000">'<span style="color: #000000">,{
templateUrl: <span style="color: #800000">'<span style="color: #800000">view1/view1.html<span style="color: #800000">'<span style="color: #000000">,controller: <span style="color: #800000">'<span style="color: #800000">View1Ctrl<span style="color: #800000">'<span style="color: #000000">
})
.when(<span style="color: #800000">'<span style="color: #800000">/view2<span style="color: #800000">'<span style="color: #000000">,{
templateUrl: <span style="color: #800000">'<span style="color: #800000">view2/view2.html<span style="color: #800000">'<span style="color: #000000">,controller: <span style="color: #800000">'<span style="color: #800000">View2Ctrl<span style="color: #800000">'<span style="color: #000000">
})
.otherwise({redirectTo: <span style="color: #800000">'<span style="color: #800000">/view1<span style="color: #800000">'<span style="color: #000000">});
}])

ui-router:

<ul <span style="color: #0000ff">class=<span style="color: #800000">"<span style="color: #800000">menu<span style="color: #800000">">

  • >state1
  • >state2
  • <span style="color: #008000">//<span style="color: #008000">js
    angular.module(<span style="color: #800000">'<span style="color: #800000">myApp<span style="color: #800000">',[<span style="color: #800000">'<span style="color: #800000">ui.router<span style="color: #800000">'<span style="color: #000000">])
    .config([<span style="color: #800000">'<span style="color: #800000">$stateProvider<span style="color: #800000">',<span style="color: #800000">'<span style="color: #800000">$urlRouterProvider<span style="color: #800000">'<span style="color: #000000">,function($stateProvider,$urlRouterProvider){
    $urlRouterProvider.otherwise(<span style="color: #800000">'<span style="color: #800000">/state2<span style="color: #800000">'<span style="color: #000000">);
    <span style="color: #008000">//<span style="color: #008000"> ui-router state配置
    $stateProvider.state(<span style="color: #800000">'<span style="color: #800000">state1<span style="color: #800000">'<span style="color: #000000">,{
    url: <span style="color: #800000">"<span style="color: #800000">/state1<span style="color: #800000">"<span style="color: #000000">,templateUrl: <span style="color: #800000">"<span style="color: #800000">view1/view1.html<span style="color: #800000">"<span style="color: #000000">,controller: <span style="color: #800000">'<span style="color: #800000">View1Ctrl<span style="color: #800000">'<span style="color: #000000">
    }).state(<span style="color: #800000">'<span style="color: #800000">state2<span style="color: #800000">'<span style="color: #000000">,{
    url: <span style="color: #800000">"<span style="color: #800000">/state2<span style="color: #800000">"<span style="color: #000000">,templateUrl: <span style="color: #800000">'<span style="color: #800000">view2/view2.html<span style="color: #800000">'<span style="color: #000000">,controller: <span style="color: #800000">'<span style="color: #800000">View2Ctrl<span style="color: #800000">'<span style="color: #000000">
    });
    }])

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读