2015-09-18 23 views
0

這是我app.js我不能夠得到一個下拉列表中選擇的項目我的下一個頁面上angularJS

var countryApp = angular.module('countryApp', ['ngRoute']); 
countryApp.controller('testController', ['$scope', '$http', '$location', function ($scope, $http, $location) { 
    $http({ 
    url: 'countries.json', 
    method: "GET", 
    headers: {'Content-Type': 'application/json'} 
    }).success(function (data) { 
    $scope.details = data; 
    console.log("data---------->", $scope.details); 
    }).error(function (data) { 
    console.log("data error---------->", $scope.details); 
    }) 
}]); 

countryApp.config(['$routeProvider', function ($routeProvider) { 
    //$httpProvider.interceptors.push('httpErrorInterceptor'); 
    $routeProvider.when('/countrydetails', { 
    templateUrl: 'partials/country-detail.html', 
    //controller: 'testController', 
    }).otherwise({ 
    redirectTo: '/countrylist', 
    templateUrl: 'partials/country-list.html', 
    controller: 'testController', 
    }); 
}]) 

countryApp.factory('testService', ['$scope', '$http', function ($scope, $http) { 
    return { } 
}]) 

這是我的國家,list.html

<h1>COUNTRIES</h1> 
<select> 
    <option ng-repeat="country in details | orderBy:'name'"> 
    <a>{{country.name}}</a> 
    </option> 
</select> 
<a href="#/countrydetails"><button >click</button></a> 

我需要在點擊按鈕時在下一頁獲取國家/地區名稱。 爲此,我有一個名爲country-details.html的html頁面 我能做些什麼在該html頁面中獲得國家的名稱。

+0

我的國家,details.html頁面看起來像

{{country.name}}

Rems

+0

你不能注入'工廠 –

回答

0

創建了一個模擬您的場景的運動員。請檢查這是否有幫助。 http://plnkr.co/edit/hV16OB?p=preview

這部分是重要的注意事項。 $routeProvider.when('/countrydetails/:countryName',

+0

就像我們可以得到的代碼一樣......對吧? – Rems

+0

是的。你可以試試。 – Mahesh

+0

http://plnkr.co/edit/i9yaeyV9UoVQuseHoh9Q?p=info可以ü檢查this.i需要國家在一個選擇boxand然後採取名稱和代碼到下一頁 – Rems

0

你可以像這樣在href: <a href="#/countrydetails/{{contry.name}}">

設爲 $routeProvider.when('/countrydetails/:name', {

然後在接下來的頁面控制器的途徑,注入$routeParams

controller('countryDetailsController', function ($scope, $routeParams) { 
    $scope.countryName = $routeParams.name 
}); 

然後,在你html簡單使用{{countryName}}

1

設置你的路線喜歡

$routeProvider.when('/countrydetails/:name', { 
templateUrl: 'partials/country-detail.html', 
}); 

更改HTML喜歡。

<a ng-href="#/countrydetails/{{country.name}}"> 
<button >click</button></a> 

最好實現命中控制器函數來使用正確的路由,但它是你實際需要的。

+0

在$ scope'第一件事,我沒有得到引導到下一個頁面,而這樣做 – Rems

+0

我已更新ans –

+0

仍然沒有變化......當我點擊某個國家然後點擊按鈕時我得到的那個國家已經在那裏看到了 – Rems

相關問題