Git Product home page Git Product logo

Comments (3)

Wscats avatar Wscats commented on May 29, 2024
<!DOCTYPE html>
<html ng-app="wsscat">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <header>wsscat的应用</header>
        <div ui-view></div>
    </body>
    <style></style>
    <script type="text/javascript" src="../js/angular.js" ></script>
    <script type="text/javascript" src="angular-ui-router.js"></script>
    <script>
        /* 使用ui-router来进行路由定义,需要注入ui.router模块 */
        var app = angular.module('wsscat', ['ui.router']);
        /* 注入$stateProvider,$urlRouterProvider */
        app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
            /* 使用when来对一些不合法的路由进行重定向 */
            $urlRouterProvider.when('', '/main');
            /* 通过$stateProvider的state()函数来进行路由定义 */
            $stateProvider.state('main', {
                url: '/main',
                templateUrl: 'views/main.html',
                controller: 'mainCtrl'
            }).state('main.a', {
                url: '/pageMain1',
                templateUrl: 'views/pageMain1.html',
                controller: 'pageMain1Ctrl'
            }).state('main.b', {
                url: '/pageMain2/:id',
                templateUrl: 'views/pageMain2.html',
                controller: 'pageMain2Ctrl'
            })
        }]);
        //路由地址为#/main
        app.controller('mainCtrl', function($scope) {
            $scope.name = 'wsscat';
        })

        //路由地址为#/main/pageMain1
        app.controller('pageMain1Ctrl',function($scope){
            $scope.name = 'abc'
        })

        app.controller('pageMain2Ctrl',function($scope,$state){
            $scope.name = 'cbd'
            console.log($state.params);
        })
    </script>

</html>

from angular-tutorial.

Wscats avatar Wscats commented on May 29, 2024

切换页面的时候,链接变色

首先我们用app.run方法在每次我们进入到对应路由前获取我们当前路由的地址
这里用到$location服务获取路由地址,注意我这里是用了ui-router,所以是$stateChangeSuccess,如果是ng-router那就是$routerChangeSuccess

app.run(function($rootScope, $location) {
                $rootScope.$on('$stateChangeSuccess',
                    function(data) {
                        console.log($location.path())
                        $rootScope.path = "#"+$location.path()
                })
})

然后在对应的DOM结构中写上这样来切换这个类
<a ng-href="#/main/pageMain1" class="{{'#/main/pageMain1'==path?'red':''}}">pageMain1</a>

from angular-tutorial.

Wscats avatar Wscats commented on May 29, 2024

ng路由

加载路由依赖,放在angular框架后面

<script type="text/javascript" src="../js/angular.js"></script>
<script type="text/javascript" src="../js/angular-route.js"></script>

$location.path

$routeProvider
两个核心方法when()otherwise()

  • controller //function或string类型。在当前模板上执行的controller函数,生成新的scope
  • controllerAs //string类型,为controller指定别名
  • template //string或function类型,视图所用的模板,这部分内容将被ngView引用
  • templateUrl //string或function类型,当视图模板为单独的html文件或是使用了<script type="text/ng-template">定义模板时使用
  • resolve //指定当前controller所依赖的其他模块
  • redirectTo //重定向的地址

from angular-tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.