【问题标题】:Taking a link title from a query parameter & displaying it somewhere on the page从查询参数中获取链接标题并将其显示在页面的某处
【发布时间】:2017-04-03 12:24:01
【问题描述】:

知道如何从查询参数中获取链接标题并将其显示在登录页面的某个位置吗?我在想一个标题,上面写着“欢迎{无论链接标题是什么},并根据点击的链接而变化。

<a ng-click="link.clicks=link.clicks+1" href="#/landing?=link={{link.linktitle}}"><span>{{ link.linktitle }}</span></a>

我的链接在浏览器中显示如下:

http://localhost:3000/#/landing?=broncos

我只需要 Broncos 部分或任何链接标题。

它应该看起来像这样: example

所有繁重的功能都发生在 controller.js 文件中,但我需要在 HTML 页面上显示“(链接标题)太棒了!部分

【问题讨论】:

  • 您可以使用 $location.search().link 获取查询参数
  • 嗨,Muhammed,我试过了,但我不知道如何将它缩小到我需要的确切参数,因为我需要定位链接内的链接。
  • 你能告诉我当你只打印 $location.search() 时你得到了什么吗?
  • 这是我使用的代码:$scope.location = $location; $scope.$watch('location.search()', function() { $scope.target = ($location.search()).link; }, true ); $scope.changeTarget = function(name) { $location.search('link', name); }
  • 你能打印 $location.search() 吗?

标签: javascript angularjs


【解决方案1】:

你可以试试这个:

var str = "http://localhost:3000/#/landing?=link%3Dbroncos";
var pagenum = str.match(/link(.*)/)[1];
alert(pagenum); // %3Dbroncos

https://jsfiddle.net/7nbLs5ww/

编辑:

var str = "localhost:3000/#/landing?=broncos";
var pagenum = str.match(/landing\?\=(.*)/)[1];
alert(pagenum); // broncos

https://jsfiddle.net/7nbLs5ww/1/

编辑 2:

var str = "http://www.your_url.com/landing/?link=wolverines";
var pagenum = str.match(/\?link\=(.*)/)[1];
document.getElementById("result").innerHTML = pagenum[0].toUpperCase() + pagenum.slice(1)+" are awesome!";

https://jsfiddle.net/7nbLs5ww/2/

【讨论】:

  • 简化您的代码并将其发布...上面的代码有效,但您需要根据您的代码进行调整...
  • 是的当然..因为“=link”错过了..我对答案做了更新
  • 现在我得到“无法获取 null 的 innerHTML”。我在控制器内完成所有的 javascript。
  • 我在上面的问题中发布了一个最终结果应该是什么样子的示例^感谢您的帮助@Konstantinos
  • 当我在 jfiddle 中练习时它工作正常,但是当我将它添加到我的代码中时它什么也不做。我的登陆页面的 HTML 中有 div,我的 controller.js 文件中有代码。我唯一能想到的就是登陆页面看不到代码,因为它可能超出了范围。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2021-05-07
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
相关资源
最近更新 更多