使用ng-view
和$routeProvider
时,可以注入$routeParams
以获取路径的值,如/foo/:id/:user/:item
。有什么方法可以在路径中设置这些参数吗?像$routeParams.id = 3
之类的东西,然后反映在url中。
我知道这个效果可以通过$location.path()
来实现,但我希望能够进行更高级别的抽象,不需要字符串操作。
使用ng-view
和$routeProvider
时,可以注入$routeParams
以获取路径的值,如/foo/:id/:user/:item
。有什么方法可以在路径中设置这些参数吗?像$routeParams.id = 3
之类的东西,然后反映在url中。
我知道这个效果可以通过$location.path()
来实现,但我希望能够进行更高级别的抽象,不需要字符串操作。
以下是我设法解决问题的方法。
控制器:
app.controller('MainCtrl', [
'$scope', '$log', '$route', '$routeParams', '$location',
function(scope, console, route, routeParams, location) {
console.log('MainCtrl.create');
scope.route = route;
scope.routeParams = routeParams;
scope.location = location;
//1. This needs to be enabled for each controller that needs to watch routeParams
//2. I believe some encapsulation and reuse through a service might be a better way
//3. The reference to routeParams will not change so we need to enable deep dirty checking, hence the third parameter
scope.$watch('routeParams', function(newVal, oldVal) {
angular.forEach(newVal, function(v, k) {
location.search(k, v);
});
}, true);
}]);
模块声明+路由定义:
var app = angular.module('angularjs-starter', [], [
'$routeProvider', '$locationProvider',
function(routeProvider, locationProvider) {
routeProvider.when('/main', {
template : 'Main',
controller : 'MainCtrl',
reloadOnSearch : false
});
} ]);
模板:
<body ng-controller="MainCtrl">
<a href="#/main">No Params</a>
<a href="#/main?param1=Hello¶m2=World!">With Params</a>
<div ng-view></div>
<p>
<span>param1</span>
<input type="text" ng-model="routeParams['param1']">
</p>
<p>
<span>param2</span>
<input type="text" ng-model="routeParams['param2']">
</p>
<pre>location.path() = {{location.path()}}</pre>
<pre>routeParams = {{routeParams}}</pre>
</body>
演示:
参考:
同样,如果你想用$ URL参数双向数据绑定位置,这个si多功能将有所帮助。
bind_var_to_urlparam = function(variable_name, scope){
// initial loading from urlparams and then
// set a watch function to update the urlparams on update
scope[variable_name] = $location.search()[variable_name];
scope.$watch(variable_name, function(newVal, oldVal){
var search_obj = {};
search_obj[variable_name] = newVal;
$location.search(search_obj);
});
}
我用这从一个共享服务,这就是为什么范围在传递
我使用服务来创建一个双向绑定:
angular.module('app').service('urlBinder', ['$location', function($location) {
this.bindOnScope = function(scope, name, urlParamName) {
// update the url when the scope variable changes
var unhookUrlUpdater = scope.$watch(name, function(newValue, oldValue) {
if (!angular.equals(oldValue, newValue)) {
$location.search(urlParamName, newValue);
}
});
// update the scope variable when the url changes
var unhookScopeUpdater = scope.$on('$locationChangeSuccess', function() {
var value = $location.search()[urlParamName];
if (!angular.equals(scope[name], value)) {
scope[name] = value;
}
});
// return a function to remove the hooks. Note that since these hooks are set up on the scope passed in, if that scope gets destroyed (e.g. because the user went to a different page and the controller is no longer present), then the hooks will be removed automatically.
return function() {
unhookUrlUpdater();
unhookScopeUpdater();
};
};
// the same thing but using getter/setter functions for when you want to bind to something not on the scope
this.bind = function(scope, getter, setter, urlParamName) {
var unhookUrlUpdater = scope.$watch(getter, function(newValue) {
$location.search(urlParamName, newValue);
});
var unhookScopeUpdater = scope.$on('$locationChangeSuccess', function() {
var value = $location.search()[urlParamName];
if (!angular.equals(getter(), value)) {
setter(value);
}
});
return function() {
unhookUrlUpdater();
unhookScopeUpdater();
};
};
}]);
在你的控制器:
angular.module('app').controller('ctrl', ['$scope', function($scope) {
// if binding to something on the scope:
urlBinder.bindToScope($scope, 'something', 'url-name');
$scope.something = 'test';
// or if the variable you want to bind isn't on the scope:
var someVariable;
urlBinder.bind(
$scope,
function() { return someVariable; },
function(value) { someVariable = value; },
'url-name');
}]);
然后可以例如绑定一个输入网址:
<div ng-controller="ctrl">
<input type="number" ng-model="something" />
</div>
您可能还需要设置reloadOnSearch : false
在路线的配置,因为你不想让控制器范围内被摧毁和重建每当搜索位的URL变化。
我喜欢它!但请注意,这不是真正的双向数据绑定。当函数被调用时,范围变量被设置为URL参数的当前值,之后,当范围变量改变时,URL参数被更新。如果两个作用域绑定到相同的URL参数,则更改该作用域的一个作用域不会更新另一个作用域中的变量。尽管这对于许多目的来说已经足够 – tobek
这是真的,关于不同作用域绑定到相同的URL参数的好处。 –