如何使第一个链接打开一个特定的链接
How to make the first link open a specific link
我有将打开html中定义的链接的表,但我想使第一个链接打开一个不同的链接,如:john.com,这是可能的吗?
所以名字:John将打开john.com
html:<div ng-controller="MyCtrl">
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in data">
<th><a href="">{{person.name}}</a></th>
<th>{{person.lastName}}</th>
<th ng-click="colorRow($index)" ng-class="{yellow : $index == row}" ng-show="$index != row ">{{person.email}}</th>
<th ng-show="$index == row" class="yellow"> Cliked</th>
</tr>
</tbody>
</table>
</div>
</div>
js:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.row = -1;
$scope.data = [{
"email": 'john@example.com',
name: "John",
lastName: "Doe"
}, {
"email": 'mary@example.com',
name: "elton",
lastName: "Junior"
}, {
"email": 'july@example.com',
name: "mark",
lastName: "Junior"
}, ]
$scope.colorRow = function(index) {
$scope.row = index;
console.log("Hey Joe", index)
}
}
css: .odd {
background-color: white;
}
.even {
background-color: grey;
}
.yellow {
background-color: yellow;
color: green;
}
http://jsfiddle.net/71rqq1o1/22/您可以通过检查html文件中的$index
语句来实现这一点,例如:
<th><a href="{{$index === 0 ? 'http://google.com' : 'http://'+person.name}}">{{person.name}}</a></th>
但是我建议你不要使用html文件中的逻辑,而在html中使用视图模型之类的东西。你必须在控制器中单独确认你的数据。
像这样:
function toViewModel(data) {
return data.map((d, i) => {
let obj = {};
if (i === 0) {
obj.link = "http://google.com";
} else {
obj.link = `http://${d.name}.com`;
}
obj.name = d.name; // you can use some libs to extend objects (e.g. lodash) instead of reassigning each attr
obj.lastName = d.lastName;
obj.email = d.email;
return obj;
});
}
$scope.viewdata = toViewModel(mydata);
在html中使用你的视图模型,只是使用对象的纯属性,没有任何逻辑。
<th><a href="{{person.link}}">{{person.name}}</a></th>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 为什么window.open不打开一个以变量形式给出的链接
- 在javascript中的xml中创建一个链接
- 有可能在来自链接的警报中有一个值吗
- 当页面包含指向一个图像的多个链接时,fancyBox会复制缩略图辅助图像
- 如何制作href链接和<李>在一个包装中
- PNG图像被覆盖,但每个图像都有一个链接可以更改,每次一个-Javascript/jQuery/CSS
- 链接到手风琴中的另一个选项卡,嵌入的链接将不起作用
- 悬停时将一个链接更改为两个
- Javascript一页滚动脚本滚动到每个点击的链接一个接一个
- 如何破解LiveScript以便在html中链接一个单独的文件
- 如何链接一个表单输出到一个谷歌融合表查询使用javascript
- 如何从URL链接一个散列链接到Javascript内容
- 如何给当前页面's链接一个禁用的外观
- 需要帮助链接一个按钮到wordpress主题自定义器的功能
- 什么是正确的方式来链接一个按钮到谷歌地图
- 链接一个本地函数
- Angular的承诺(关于一个承诺?)或者如何在$http之后链接一个.then.成功的角度
- js试图在一个特定的日期链接一个时间表