【发布时间】:2015-12-05 14:21:42
【问题描述】:
我对 Angular 还很陌生,并且正在与一个客户合作,该客户需要一个下拉菜单,允许用户选择他们的社区,然后将其保存在 cookie 中以在返回时加载。我可以保存 cookie,但无法让下拉选择的社区加载正确的模板。
这里是html:
<select id="mNeighborhood" ng-model="mNeighborhood" ng-options="neighborhood.id as neighborhood.name for neighborhood in neighborhoods" ng-change="saveCookie()"></select>
然后,我在 html 中添加了以下内容:
<div ng-view=""></div>
这里是 app.js 代码:
var app = angular.module('uSarasota', ['ngCookies', 'ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
template: '<div><h1 style="margin: 200px;">This is our main page</h1></div>'
})
.when('/downtown', {
templateUrl: "templates/downtown.html"
})
.otherwise({
template: '<div><h1 style="margin: 200px;""><strong>NOTHING TO SEE HERE</strong></h1></div>'
})
});
//Select Neighborhood
app.controller('myNeighborhood', ['$scope', '$cookies', function($scope, $cookies) {
$scope.neighborhoods = [{
name: "My Neighborhood",
id: 0
}, {
name: "All of Sarasota",
id: 1
}, {
name: "Downtown",
url: "/downtown",
id: 2,
}, {
name: "North Sarasota",
id: 3
}, {
name: "Lakewood Ranch",
id: 4
}, {
name: "Longboat Key",
id: 5
}, {
name: "St. Armands Circle",
id: 6
}, {
name: "Southside Village",
id: 7
}, {
name: "Siesta Key",
id: 8
}, {
name: "Gulf Gate",
id: 9
}];
//Set Cookie so when user returns to site, it will be on their neighborhood
$scope.mNeighborhood = parseInt($cookies.get('sNeighborhood')) || 0;
$scope.saveCookie = function() {
$cookies.put('sNeighborhood', $scope.mNeighborhood);
};
}]);
这一切都可以很好地保存和加载用户选择,但我无法找到基于选择获取模板的解决方案。那么,我是否应该将 url 添加到每个社区的数组中,如果是,我如何获取链接?
【问题讨论】:
标签: javascript angularjs ng-options dropdown angularjs-ng-route