How/when to use ng-click to call a route?

前端 未结 8 988
时光说笑
时光说笑 2020-11-30 16:32

Suppose you are using routes:

// bootstrap
myApp.config([\'$routeProvider\', \'$locationProvider\', function ($routeProvider, $locationProvider) {

    $rout         


        
相关标签:
8条回答
  • 2020-11-30 17:11

    Routes monitor the $location service and respond to changes in URL (typically through the hash). To "activate" a route, you simply change the URL. The easiest way to do that is with anchor tags.

    <a href="#/home">Go Home</a>
    <a href="#/about">Go to About</a>
    

    Nothing more complicated is needed. If, however, you must do this from code, the proper way is by using the $location service:

    $scope.go = function ( path ) {
      $location.path( path );
    };
    

    Which, for example, a button could trigger:

    <button ng-click="go('/home')"></button>
    
    0 讨论(0)
  • 2020-11-30 17:12

    Another solution but without using ng-click which still works even for other tags than <a>:

    <tr [routerLink]="['/about']">
    

    This way you can also pass parameters to your route: https://stackoverflow.com/a/40045556/838494

    (This is my first day with angular. Gentle feedback is welcome)

    0 讨论(0)
  • 2020-11-30 17:13

    You can use:

    <a ng-href="#/about">About</a>
    

    If you want some dynamic variable inside href you can do like this way:

    <a ng-href="{{link + 123}}">Link to 123</a>
    

    Where link is Angular scope variable.

    0 讨论(0)
  • 2020-11-30 17:14

    I used ng-click directive to call a function, while requesting route templateUrl, to decide which <div> has to be show or hide inside route templateUrl page or for different scenarios.

    AngularJS 1.6.9

    Lets see an example, when in routing page, I need either the add <div> or the edit <div>, which I control using the parent controller models $scope.addProduct and $scope.editProduct boolean.

    RoutingTesting.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Testing</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-route.min.js"></script>
        <script>
            var app = angular.module("MyApp", ["ngRoute"]);
    
            app.config(function($routeProvider){
                $routeProvider
                    .when("/TestingPage", {
                        templateUrl: "TestingPage.html"
                    });
            });
    
            app.controller("HomeController", function($scope, $location){
    
                $scope.init = function(){
                    $scope.addProduct = false;
                    $scope.editProduct = false;
                }
    
                $scope.productOperation = function(operationType, productId){
                    $scope.addProduct = false;
                    $scope.editProduct = false;
    
                    if(operationType === "add"){
                        $scope.addProduct = true;
                        console.log("Add productOperation requested...");
                    }else if(operationType === "edit"){
                        $scope.editProduct = true;
                        console.log("Edit productOperation requested : " + productId);
                    }
    
                    //*************** VERY IMPORTANT NOTE ***************
                    //comment this $location.path("..."); line, when using <a> anchor tags,
                    //only useful when <a> below given are commented, and using <input> controls
                    $location.path("TestingPage");
                };
    
            });
        </script>
    </head>
    <body ng-app="MyApp" ng-controller="HomeController">
    
        <div ng-init="init()">
    
            <!-- Either use <a>anchor tag or input type=button -->
    
            <!--<a href="#!TestingPage" ng-click="productOperation('add', -1)">Add Product</a>-->
            <!--<br><br>-->
            <!--<a href="#!TestingPage" ng-click="productOperation('edit', 10)">Edit Product</a>-->
    
            <input type="button" ng-click="productOperation('add', -1)" value="Add Product"/>
            <br><br>
            <input type="button" ng-click="productOperation('edit', 10)" value="Edit Product"/>
            <pre>addProduct : {{addProduct}}</pre>
            <pre>editProduct : {{editProduct}}</pre>
            <ng-view></ng-view>
    
        </div>
    
    </body>
    </html>

    TestingPage.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .productOperation{
                position:fixed;
                top: 50%;
                left: 50%;
                width:30em;
                height:18em;
                margin-left: -15em; /*set to a negative number 1/2 of your width*/
                margin-top: -9em; /*set to a negative number 1/2 of your height*/
                border: 1px solid #ccc;
                background: yellow;
            }
        </style>
    </head>
    <body>
    
    <div class="productOperation" >
    
        <div ng-show="addProduct">
            <h2 >Add Product enabled</h2>
        </div>
    
        <div ng-show="editProduct">
            <h2>Edit Product enabled</h2>
        </div>
    
    </div>
    
    </body>
    </html>

    both pages - RoutingTesting.html(parent), TestingPage.html(routing page) are in the same directory,

    Hope this will help someone.

    0 讨论(0)
  • 2020-11-30 17:18

    just do it as follows in your html write:

    <button ng-click="going()">goto</button>
    

    And in your controller, add $state as follows:

    .controller('homeCTRL', function($scope, **$state**) {
    
    $scope.going = function(){
    
    $state.go('your route');
    
    }
    
    })
    
    0 讨论(0)
  • 2020-11-30 17:22

    Using a custom attribute (implemented with a directive) is perhaps the cleanest way. Here's my version, based on @Josh and @sean's suggestions.

    angular.module('mymodule', [])
    
    // Click to navigate
    // similar to <a href="#/partial"> but hash is not required, 
    // e.g. <div click-link="/partial">
    .directive('clickLink', ['$location', function($location) {
        return {
            link: function(scope, element, attrs) {
                element.on('click', function() {
                    scope.$apply(function() {
                        $location.path(attrs.clickLink);
                    });
                });
            }
        }
    }]);
    

    It has some useful features, but I'm new to Angular so there's probably room for improvement.

    0 讨论(0)
提交回复
热议问题