I have below string :

我有以下字符串:

"/root/get";

Now I am generating a query string in above string with 1 scope variable but problem is when the value of that variable change then that new value is not getting update in my URL automatically.

现在我在上面的字符串中生成一个带有1个范围变量的查询字符串,但问题是当该变量的值发生变化时,新值不会自动在我的URL中获得更新。

You can see in below demo that I have 2 button Update and Check. In update I am generating query string and on check button I am updating the value of scope variable but that is not getting reflected in my URL.

您可以在下面的演示中看到我有2个按钮更新和检查。在更新中我生成查询字符串,并在检查按钮上我更新范围变量的值,但这没有反映在我的URL中。

I am not getting why this is happening.

我不明白为什么会发生这种情况。

Expected output when I click on check button without calling generateQueryParameters method:

单击检查按钮而不调用generateQueryParameters方法时的预期输出:

/root/get?no=2

var app = angular.module("myApp", []);
        app.controller("myController", function ($scope) {
        
        $scope.no = 1;
        $scope.str = "/root/get";
     
        $scope.update = function (data) {
          $scope.str=generateQueryParameters($scope.str,
               "no",$scope.no);
               console.log($scope.str);
        };
           

            $scope.check = function () {
              $scope.no=2;
              console.log($scope.str);
            };
               
    function generateQueryParameters(url,name,value)
    {
        var re = new RegExp("([?&]" + name + "=)[^&]+", "");
        function add(sep) {
            url += sep + name + "=" + encodeURIComponent(value);
        }
        function change() {
            url = url.replace(re, "$1" + encodeURIComponent(value));
        }
        if (url.indexOf("?") === -1) {
            add("?");
        } else {
            if (re.test(url)) {
                change();
            } else {
                add("&");
            }
        }
        return url;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
    <input type="button" value="Update" ng-click="update()">
    <input type="button" value="Check" ng-click="check()">
</div>

更多相关文章

  1. 开心菜鸟系列----变量的解读(javascript入门篇)
  2. Javascript字符串替换为动态创建的模式。
  3. 如何使用变量创建数组?
  4. 求助 急 js替换字符串问题
  5. 如何判断字符串是一个字符串化的JSON对象
  6. JavaScript相当于Ruby的字符串#扫描
  7. 11、javascript中字符串常用操作总结、JS字符串操作大全
  8. 从占用转义字符的字符数组创建字符串
  9. Angularjs:如何将范围变量传递给指令?

随机推荐

  1. 弱智入门Linux——拉高CPU使用率
  2. gdb捕获syscall条件和字符串比较
  3. Linux命令-网络命令:netstat
  4. 学linux向那方面发展比较好
  5. 【Linux】Linux下使用wget 命令下载JDK7
  6. 我是这样学习Linux下C语言编程的-编译命
  7. Debian 7开启ssh、telnet
  8. Linux如何查找大文件
  9. linux中ioremap与ioremap_cachable的区别
  10. 应用程序利用init.rc service获得root权