2017-05-30 43 views
0

我想單向綁定一個URL參數'電子郵件'使用Angular的輸入字段的值。期望的效果是,只要URL中的電子郵件參數發生更改,輸入字段就會更改爲與新的電子郵件參數vaue匹配。我在我的js文件這樣做:角 - 綁定URL參數輸入文本不工作

$scope.inputEmail = $location.search()['email'] 

要修改我使用了NG-型號,輸入字段的值「inputEmail」。當我這樣做時,當我更改URL電子郵件參數時,輸入字段的值不會更改。我究竟做錯了什麼?

回答

0

我發現我的問題,我忘了包括$位置和它的依賴關係e我定義了我的組件控制器。謝謝您的幫助!

1

不知道這是否可以幫助車針從角文檔 https://docs.angularjs.org/api/ng/service/ $位置

// given URL http://example.com/#/some/path?foo=bar&baz=xoxo 
var searchObject = $location.search(); 
// => {foo: 'bar', baz: 'xoxo'} 

然後,你的例子:

$scope.auxSearch = $location.search(); 
$scope.inputEmail = $scope.auxSearch.email; 

,或許也可能是公正的,沒有如果這肯定無論如何將工作:

$scope.inputEmail = $location.search().email; 
0

你可以嘗試thi S:

$scope.$on('$routeUpdate', function(){ 
    $scope.inputEmail = $location.search()['email'] 
}); 

注意,您必須設置reloadOnSearchfalse路線上$routeUpdate可用

否則,你可以使用$ rootscope:

$rootScope.$on('$locationChangeSuccess', function(event){ 
     $scope.inputEmail = $location.search()['email'] 
}) 

這將更新模型值,當你改變你的查詢參數