范围变量值的变化没有反映在我的字符串中
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>
更多相关文章
- 开心菜鸟系列----变量的解读(javascript入门篇)
- Javascript字符串替换为动态创建的模式。
- 如何使用变量创建数组?
- 求助 急 js替换字符串问题
- 如何判断字符串是一个字符串化的JSON对象
- JavaScript相当于Ruby的字符串#扫描
- 11、javascript中字符串常用操作总结、JS字符串操作大全
- 从占用转义字符的字符数组创建字符串
- Angularjs:如何将范围变量传递给指令?