0

我正在寻找从控制器更新范围值的“ng-href”url。最初在页面加载 url 上插入很好。但是当我更改应该更新 URL 的范围时

<a ng-href=={{selectedValue}} /a>

//in my controller
$scope.selectedValue = "www.google.com"

//need to update this url in this function
otherButtonFn = function(){
//want to Update the url here
$scope.selectedValue =  "www.yahoo.com"
}

//ng-href is loading initial url but not updating when $scope is called
  <a ng-href=="www.google.com" href=""www.google.com" /a>

在这里,我想在调用该函数时使用“www.yahoo.com”更新网址。

4

2 回答 2

1

问题在于锚标签有双 == 并且没有 "" 围绕它。请参阅下面的更新示例:

<a ng-href="{{selectedValue}}">Link Name</a>

$scope.selectedValue = "www.google.com";


$scope.otherButtonFn = function(){
   $scope.selectedValue =  "www.yahoo.com";
}
于 2016-06-16T19:52:16.293 回答
0

angular.module('myApp',[])
.controller('TestCtrl', function($scope){
  $scope.something = "somewhere";
  $scope.changeSomething = function(){
    $scope.something = "somewhere else";
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="TestCtrl">
    <a ng-href="{{something}}">{{something}}</a>
    <button ng-click="changeSomething()">Change</button>
  </div>
</div>

无法重现所述问题。

于 2016-06-16T19:47:13.440 回答