【问题标题】:How can i go to another page using angularjs我如何使用 angularjs 转到另一个页面
【发布时间】:2017-03-28 01:53:02
【问题描述】:

我正在尝试从 url 中的一个页面传递数据并从 url 进入新页面,例如我想在 url 中添加 10,20。就像在给定的示例中,我在 sumurl 中传递 10,20 它在 div ng-view 中显示它的总和,但我想在 sumurl.html 或 input.html 中显示它

var app=angular.module('myapp',['ngRoute']);
    app.config(['$routeProvider',function($routeProvider){
        
        $routeProvider
            .when('/sumurl/:a/:b',{
            templateUrl:'sumurl.html',
            controller:'sumurl'
        })
            .when('/input',{
            templateUrl:'input.html',
            controller:'input'
        })
            .when('/',{
            template:'Welcome to my app'
        }).otherwise({
            template:'Not Available'
        })
    }]);
    
    app.controller('sumurl',['$scope','$routeParams',function($scope,$routeParams){
        $scope.n1=$routeParams.a;
        $scope.n2=$routeParams.b;
        
    }]);
    
    app.controller('input',['$scope','$location','$interpolate',function($scope,$location,$interpolate){
        $scope.n1=0;
        $scope.n2=0;
        
        $scope.dosum=function(){
            var url=$interpolate('/sumurl/{{n1}}/{{n2}}')($scope);
                //console.log(url);
            $location.path(url);
        }
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<div ng-app="myapp">
<ul>
<li><a href="#/sumurl/10/20">some url</a></li>
<li><a href="#/input">input</a></li>
<li></li>
</ul>
    <div ng-view>
    
    </div>
    
</div>

Sumurl 和 input 是两个 html 文件

sumurl.html

<div>
    a = {{n1}}<br>
    b = {{n2}}<br>
    Sum = {{(n1-0)+(n2-0)}}
</div>

还有 input.html

<div>
    n1 = <input type="number" ng-model="n1"/>
    n2 = <input type="number" ng-model="n2"/>
    <button ng-click="dosum()">Sum</button>
    
</div>

【问题讨论】:

    标签: javascript jquery angularjs url-routing


    【解决方案1】:

    注入$location服务,然后你就可以做$location.href = &lt;your target URL&gt;

    【讨论】:

    • 你能给我举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2015-03-12
    • 2014-10-17
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多