2015-06-19 61 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>