用于更改 URL 的 AngularJS 选项列表



我想根据选择的选项重定向到另一个页面,但它甚至不是控制台日志记录。我也尝试使用ng-change。

.HTML:

<select ng-options="option for option in selectCate" ng- 
model="selectedCate" ng-click="selectedCateChange()">
</select>

.JS:

$scope.selectCate = ['Amenities', 'Attractions'];
$scope.selectedCate = $scope.selectCate[0];
$scope.selectedCateChange = function () {
if ($scope.selectedCateChange == "Amenities")
{
console.log("aa")
}
else if ($scope.selectedCateChange == "Attractions")
{
$window.location.href = '#!/Attractions'
}
}

与其在<select>上使用ng-click,不如使用ng-change并在selectedCateChange中传递selectedCate。请考虑以下工作代码狙击。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedCate" ng-change="selectedCateChange(selectedCate)">
<option ng-repeat="option in selectCate" value="{{option}}">{{option}}</option>
</select>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.selectCate = ['Amenities', 'Attractions'];
$scope.selectedCateChange = function (selectedCate) {
if (selectedCate == "Amenities"){
alert("aa");
}else if (selectedCate == "Attractions"){
alert("call your router here");
}
}
});
</script>
</body>
</html>

对于重定向,您可以使用$stateProvider的角度网址。

$stateProvider
.state("Attractions", {
url: "/Attractions",
templateUrl: "app/components/layout/Attractions.html",
resolve: loader(["scripts/components/layout/AttractionsController", "scripts/components/distributed/popupModalController"]),
controller: "attractionsCtrl"
});
$state.go('Attractions');

最新更新