AngularJS Ng-route - 从另一个 url 输入 input:text 时切换到特定页面视图
AngularJS Ng-route - Switching to a particular page view when typing an input:text from another url
抱歉,标题太长了。此问题基于提供的答案
我有一个页面有两个视图
app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider) {
$routeProvider
.when('/content1', {
templateUrl: 'content1.html',
controller: 'myCtrl'
})
.when('/content2', {
templateUrl: 'content2.html',
controller: 'myCtrl'
})
.otherwise({
redirectTo: '/content1'
});
}]);
当键入 input:text 时,它会使用触发函数 searchProduct()
的 ng-change 指令切换到第二个视图,并立即使用 ng-model
指令开始搜索.
<div ng-controller="myCtrl" ng-app="myApp">
<input type="text"
ng-change="searchProduct()"
ng-model="searchFilter"
ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 500, 'blur': 0 } }">
<ng-view></ng-view>
</div>
作为 searchProduct()
函数
$scope.searchProduct = function() {
$location.path('content2');
$location.search({
searchFilter: $scope.searchFilter
});
};
一切正常。但问题是我想在另一个具有不同 url 的页面中实现相同的功能(这是为了 SEO 目的),以便在该页面中搜索时它会重定向到 content2.html,并且它还会保留在搜索框中输入的内容。
我试过设置绝对路径来实现,但是好像不行。可以看到a working plunker with the code here。提前致谢!
我建议您使用 $stateprovider 进行路由。它比路由提供者更好。你可以通过$routeProvider在网上读出它的加分。 Route 现在已经过时了。在 state provider 上你只需要重定向到 state 你的问题也将得到解决。 $state.go('content2') 或 $state.go('home')
你也可以有嵌套状态。
https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stateprovider-1
抱歉,标题太长了。此问题基于提供的答案
我有一个页面有两个视图
app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider) {
$routeProvider
.when('/content1', {
templateUrl: 'content1.html',
controller: 'myCtrl'
})
.when('/content2', {
templateUrl: 'content2.html',
controller: 'myCtrl'
})
.otherwise({
redirectTo: '/content1'
});
}]);
当键入 input:text 时,它会使用触发函数 searchProduct()
的 ng-change 指令切换到第二个视图,并立即使用 ng-model
指令开始搜索.
<div ng-controller="myCtrl" ng-app="myApp">
<input type="text"
ng-change="searchProduct()"
ng-model="searchFilter"
ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 500, 'blur': 0 } }">
<ng-view></ng-view>
</div>
作为 searchProduct()
函数
$scope.searchProduct = function() {
$location.path('content2');
$location.search({
searchFilter: $scope.searchFilter
});
};
一切正常。但问题是我想在另一个具有不同 url 的页面中实现相同的功能(这是为了 SEO 目的),以便在该页面中搜索时它会重定向到 content2.html,并且它还会保留在搜索框中输入的内容。
我试过设置绝对路径来实现,但是好像不行。可以看到a working plunker with the code here。提前致谢!
我建议您使用 $stateprovider 进行路由。它比路由提供者更好。你可以通过$routeProvider在网上读出它的加分。 Route 现在已经过时了。在 state provider 上你只需要重定向到 state 你的问题也将得到解决。 $state.go('content2') 或 $state.go('home') 你也可以有嵌套状态。
https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stateprovider-1