[英]Angular ng-href with Split method
I have a recursive variable with value 我有一个带有值的递归变量
path = <d:URL>http://www.google.com, Google</d:URL>
I want to bind this path in to angular component. 我想将此路径绑定到角度分量。 I am trying to use the below method to bind the ng-href value.
我正在尝试使用以下方法来绑定ng-href值。
<a ng-href="{{path.URL.split(",")[0]}}" target="_blank">{{path.URL.split(",")[1]}}</a>
It seems this breaks with the href but the split gets evaluated for the link title. 看来这与href中断了,但已对链接标题进行了评估。 How can I resolve this.
我该如何解决。
Thanks. 谢谢。
You can create a function to perform the split functionality. 您可以创建一个函数来执行拆分功能。 So try like this
所以尝试这样
HTML : HTML :
<a ng-href="{{createURL()}}" target="_blank">{{path.URL.split(",")[1]}}</a>
JS JS
$scope.createURL=function(path){
return path.URL.split(",")[0]
}
create a function and perform the split operations 创建一个函数并执行拆分操作
$scope.getSplitVal = function(url){
return url.split(",")[1]
}
angular.module("app",[]) .controller("ctrl",function($scope){ $scope.path = "<d:URL>http://www.google.com, Google</d:URL>" $scope.getSplitVal = function(url){ return url.split(",")[1] } })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <a ng-href="{{getSplitVal(path)}}" target="_blank">{{getSplitVal(path)}}</a> </div>
I think you need to do any string manipulations into controller and not in HTML. 我认为您需要对控制器而不是HTML进行任何字符串操作。 Its not good practice.
这不是一个好习惯。
The string "<d:URL>http://www.google.com, Google</d:URL>"
should go to: 字符串
"<d:URL>http://www.google.com, Google</d:URL>"
应转到:
var path = "<d:URL>http://www.google.com, Google</d:URL>";
$scope.path = {
URL:{
value:path.split(",")[0].replace("<d:URL>", ""),
name: path.split(",")[1].replace("</d:URL>", "")
}
};
So your HTML will look like: 因此,您的HTML将如下所示:
<a ng-href="{{path.URL.value}}" target="_blank">{{path.URL.name}}</a>
Problem caused by double quotes that breaks ng-href
when html is outputted but not handled by angular and it becomes: 由双引号引起的问题,当输出html而不是由angular处理时,双引号破坏了
ng-href
,它变成:
ng-href="{{path.URL.split("
So simply change double quotes inside of split
to single quotes: 因此,只需将
split
内的双引号更改为单引号:
<ul ng-controller="PathsController as $ctrl">
<li ng-repeat="path in $ctrl.paths">
<a
ng-href="{{ path.URL.split(',')[0].replace('<d:URL>', '') }}"
>
{{ path.URL.split(",")[1].replace('</d:URL>', '') }}
</a>
</li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.