2015-06-19 62 views
-1

我确实有点不知所措,正如前面提到的,我需要将链接放入js中的var中,以便只有在单击此项时才会打开链接。将链接设置为Js var?

的js部分看起来如下:

var items = [{ 
    'icon': 'new', 
    'name': 'test', 
    'date': 'today', 
    'user': { 
     'name': 'test', 
     'color': '#07D5E5' 
    } 
}]; 

和HTML输出的一部分是这样的:

<pager total-items="totalItems" items-per-page="itemsPerPage" 
    ng-model="currentPage" max-size="maxSize" ng change="pageChanged()"></pager> 
     <p ng-if="itemsPerPage * currentPage < totalItems"> 
     Showing {{ itemsPerPage * currentPage - itemsPerPage + 1 }} - 
    {{ itemsPerPage * currentPage }} of {{ totalItems }} total items 
     </p> 
     <p ng-if="itemsPerPage * currentPage >= totalItems"> 
     Showing {{ itemsPerPage * currentPage - itemsPerPage + 1 }} - 
     {{ totalItems }} of {{ totalItems }} total items 
     </p> 

如何在这个设定的JS链接,将打开时,有人会点击名称:测试?我不明白

+0

你喜欢在哪里放置链接?当你点击它时会发生什么? – AWolf

+0

@AWolf嗯,它应该在每个名称的js代码中,因为这只是一行manys,并且应该只在我点击Name测试时执行。点击它后,它应该打开一个链接,如http://在同一个或新标签中的东西,这并不重要。如果你需要一个真正的测试网站的例子,它目前正在运行,也许更好地理解它,打我吧 –

+0

你只显示了HTML的分页位。没有关系到你的'var items' – ethorn10

回答

0

我会建议您更新项目变量的结构,包括URL属性和目标属性:

var items = [{ 
    'icon': 'new', 
    'name': 'test', 
    'url': 'http://some.actual.url/', 
    'target': '_blank', 
    'date': 'today', 
    'user': { 
     'name': 'test', 
     'color': '#07D5E5' 
    } 
}]; 

然后在您的角度:

<span class="grid-title"> 
    <a href="{{ item.url }}" target="{{ item.target }}">{{ item.name }}</a> 
</span> 
+0

非常感谢,确实按预期工作。不幸的是我不能满足它,因为它需要15个声望;)因为我很好奇,如果它应该在_blank Tab中打开,它会是什么样子? –

0

这里是一个使用各种属性并包括您的物品以利用它们的示例:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
 
<script> 
 
    var app = angular.module('myApp', []); 
 
    app.controller('myCtrl', function($scope) { 
 
     $scope.itemsPerPage = 10; 
 
     $scope.currentPage = 1; 
 
     $scope.totalItems = 100; 
 

 
     var items = [{ 
 
      'icon': 'new', 
 
       'name': 'test', 
 
       'date': 'today', 
 
       'user': { 
 
       'name': 'test', 
 
        'color': '#07D5E5' 
 
      } 
 
     }]; 
 

 
     $scope.otherItems = [{ 
 
      'icon': 'old', 
 
       'name': 'other', 
 
       'date': 'yesterday', 
 
       'user': { 
 
       'name': 'other', 
 
        'color': '#02D0E0' 
 
      } 
 
     }]; 
 

 
     $scope.redirect = function(name) { 
 
      if (name == 'test') { 
 
       window.location.href = 'http://stackoverflow.com/questions/30948701/set-a-link-into-a-js-var'; 
 
      } 
 
     }; 
 

 
     showItems = []; 
 

 
     function init() { 
 
      for (var i = 0; i < 100; i++) { 
 
       $scope.otherItems.push(i == 5 ? items[0] : $scope.otherItems[0]); 
 
      } 
 
     } 
 
     init(); 
 

 
    }); 
 
</script> 
 
<div ng-app="myApp" 
 
    ng-controller="myCtrl"> 
 
    <pager total-items="totalItems" 
 
      items-per-page="itemsPerPage" 
 
      ng-model="currentPage" 
 
      max-size="maxSize" 
 
      ng-change="pageChanged()"></pager> 
 
    <p ng-if="itemsPerPage * currentPage < totalItems">Showing {{ itemsPerPage * currentPage - itemsPerPage + 1 }} - {{ itemsPerPage * currentPage }} of {{ totalItems }} total items</p> 
 
    <p ng-if="itemsPerPage * currentPage >= totalItems">Showing {{ itemsPerPage * currentPage - itemsPerPage + 1 }} - {{ totalItems }} of {{ totalItems }} total items</p> 
 
    <p ng-repeat="i in otherItems track by $index" 
 
     ng-if="$index < itemsPerPage * currentPage && $index > itemsPerPage * (currentPage-1)" 
 
     ng-click="redirect(i['name'])" 
 
     ng-style="{'text-decoration':(i['name'] == 'test' ? 'underline' : '')}">{{i['name']}}</p> 
 
</div> 
 
</div>