我怎样才能改变PARAMS中的网址与AngularJS? [英] How can I change params in url with AngularJS?

查看:131
本文介绍了我怎样才能改变PARAMS中的网址与AngularJS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在inputfield小费改变我对飞angularjs应用程序的URL。

例如。


  1. 我留在:
    的http://本地主机/测试/年/ 2012

  2. 我将通过输入字段年度边改至2013年该叫我yearIsChanged功能,比URL应该改为
    的http://本地主机/测试/年/ 2013

  3. 但我目前配置的网址更改为
    的http://本地主机/测试/年/ 2012 /年= 2013

我的模件的配置。

  VAR模块= angular.module('ExampleApp中')。
配置(['$ routeProvider',函数($ routeProvider){
    $ routeProvider。
        当('/测试/年/:今年,{templateUrl:'谐音/ test.html的',控制器:OverviewCtrl,reloadOnSearch:假})。
        否则({redirectTo:'/'});
}]);

控制器动作:

 函数OverviewCtrl($范围,$ routeParams,$位置){
      $ scope.yearIsChanged =功能(){
        $ location.search('年',$ scope.year);
    }
}


解决方案

$ location.search 将创建像一个网址:

 的http://本地主机/测试/年/ 2012年= 2013

这是不是你想要的。您需要使用 $ location.url()

 函数OverviewCtrl($范围,$ routeParams,$位置){
      $ scope.yearIsChanged =功能(){
        $ location.url('/测试/年/+ $ scope.year);
    }
}

I want change the url in my angularjs app on the fly by tipping in a inputfield.

e.g.

  1. I stay on: http://localhost/test/year/2012
  2. I will change on the side via a input field the year to 2013 that call my yearIsChanged function, than the url should changed to http://localhost/test/year/2013
  3. But with my current configuration the url is changed to http://localhost/test/year/2012/?year=2013

My modul configuration.

var module = angular.module('exampleApp').
config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/test/year/:year', {templateUrl: 'partials/test.html', controller: OverviewCtrl, reloadOnSearch: false}).
        otherwise({redirectTo: '/'});
}]);

Controller action:

 function OverviewCtrl($scope,$routeParams, $location) {
      $scope.yearIsChanged = function () {
        $location.search('year', $scope.year);
    }   
}

解决方案

$location.search will create an url like:

http://localhost/test/year/2012?year=2013

which is not what you want. You need to use $location.url():

function OverviewCtrl($scope,$routeParams, $location) {
      $scope.yearIsChanged = function () {
        $location.url('/test/year/' + $scope.year);
    }   
}

这篇关于我怎样才能改变PARAMS中的网址与AngularJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆