我应该如何构建一个angularjs页面与直接链接,影响页面内容
How should I build an angularjs page with direct link that impact the page content
我有一个类似于"Google搜索"的页面,它显示了后端返回的项目列表。
页面中有一个包含许多不同字段的HTML表单。用户可以更改这些字段中的一个或多个值,然后后端将返回新的相关项列表。
其中一个请求是让用户能够打开过滤结果的直接链接,到目前为止,这是通过url中的查询参数完成的,但现在我想改变这个页面,使其与angularjs异步工作。这里有一个假设,即过滤器表单中的字段可以随时更改。
那么,在angularjs中处理复杂表单并允许表单的get方法的最好方法是什么?
你总是可以在你的作用域中建立一个URL来响应用户所做的任何事情然后做<a ng-attr-href="{{view.userURL}}" target="_blank">{{view.userURL}}</a>
经过一番思考,我有了一个解决办法。
这里的一些"魔法"是在$作用域中有一个对象(名为$scope.filter)来保存所有表单输入值,因此它很容易使用,并且如果向表单添加新的输入,则不需要在控制器中对该部分进行任何修改。
angular.module('myApp')
.controller('myController', ['$scope', '$http' '$location', function($scope, $http, $location) {
$scope.filter = {};
$scope.init = function(){
// $location.search() will return an object with the params,
// those values are set to $scope.filter, and the filter values are initialized in case of direct link to the page
$scope.filter = $location.search();
$scope.executeFilter();
};
// doing the job of fetching the data I want to present,
// using the filter values as a Json in that server call
$scope.executeFilter = function(){
// making sure to update the url with the current filter values,
// so the user is able to copy the relevant url for a later use
$location.search($scope.filter);
var paramsAsJson = angular.toJson($location.search());
$http.post('my-url', paramsAsJson).success(function(data) {
// update view
});
};
$scope.init();
}]);
这是一个示例视图:
<form ng-submit="executeFilter()">
<input name="some_text" type="text" ng-model="filter.some_text">
<!-- more inputs here -->
// it will work even with multiple select
// (will pass to the server a json with array of selected values)
<select multiple="multiple" name="some_options" ng-model="filter.some_options">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<button>Filter</button>
</form>
所以现在如果一个用户试图打开这个页面的url像这样的www.my-url.com/?some_text=foo
的输入指令ng-model="filter.some_text"
将包含'foo'页面加载后,与该参数的服务器请求将作出
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 如何使用jQuery自动打开页面上的所有链接
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- jquery动画可以通过编程链接吗
- 在单击href链接的同时下载文件
- 尽管链接成功并已成功下载,但未找到NPM模块
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 单击页面上的链接后高度发生变化
- 将纯文本URL转换为可单击链接
- 在触摸设备上的悬停状态后启用链接,而不会影响正常滚动
- 如何影响营销渠道规则.正在处理下面的链接
- Rails 4删除受模板JS影响的链接
- 我应该如何构建一个angularjs页面与直接链接,影响页面内容
- 我创建了一个母版页,其中有标题.在标题部分有一些链接.我想在不影响母版页的情况下突出显示这些链接
- 我如何在不影响布局的情况下将链接放在选项1,选项2等
- 文字装饰不影响超链接
- 将鼠标悬停在链接上以影响另一个链接
- 如何在不影响页面其他功能的情况下向我的 HTML 页面添加链接
- 阻止超链接的默认操作会影响浏览器历史记录吗?