2015-08-26 31 views
1

我試圖通過ng-click和我的控制器內部的一個函數鏈接到不同的頁面/視圖。我不用href做,因爲以後它應該是一個右鍵單擊事件。但我的代碼不起作用。我嘗試了很多,並且搜索了很多頁面,但是我找不到任何解決方案。感謝您的幫助:)ng-click無法鏈接到另一個視圖

game.html

<div class="container" ng-controller="MainController"> 
 
\t <div class="handcards" ng-repeat="card in stdCards"> 
 
    \t <a ng-click="goToCardDetail()" href="">{{ card.name }}</a> 
 
    </div> 
 
</div>

MainController.js

app.controller('MainController', ['$scope', 'stdcards', '$location', function($scope, stdcards, $location) { 
 
\t stdcards.success(function(data) { 
 
\t \t $scope.stdCards = data; 
 
\t }); 
 
\t 
 
\t $scope.goToCardDetail = function() { 
 
\t \t $location.path('#/cards/0'); 
 
\t }; 
 
}]);

app.js

'use strict'; 
 

 
var app = angular.module('myApp', ['ngRoute']); 
 

 
app.config(function ($routeProvider) { 
 
\t $routeProvider 
 
\t \t .when('/', { 
 
\t \t \t controller: 'MainController', 
 
\t \t \t templateUrl: 'templates/game.html' 
 
\t \t }) 
 
\t \t .when('/cards/:id', { 
 
\t \t \t controller: 'CardController', 
 
\t \t \t templateUrl: 'templates/card.html' 
 
\t \t }) 
 
\t \t .otherwise({ 
 
\t \t \t redirectTo: '/' 
 
\t \t }); 
 
});

+0

考慮使用ngHref代替ngClick:https://docs.angularjs.org/api/ng/directive/ngHref – HankScorpio

回答

1

你並不需要包括#當你調用$location.path()。因此,它應該只是

$location.path('/cards/0'); 
+0

謝謝:)你救了我一晚,隨後的日子裏;) – Oldies93

相關問題