2017-08-24 232 views
0

我想根據下拉列表中的選擇更改導航欄中的下拉列表名稱。我發現HTML標籤不支持ng-click。我曾嘗試過,但它不起作用。幫助我找到解決這個問題的辦法。需要使用angularJs的解決方案,但最糟糕的情況是jQuery是可能的。根據下拉列表中的選擇更改下拉列表名稱

我的代碼:

HTML

 <div id="navbar" class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav navbar-right"> 
       <li><a href="#">Link</a></li> 
       <li class="dropdown"> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Current: {{currentApplication}}</a> 
        <ul class="dropdown-menu" ng-repeat="application in applications" ng-click="changeApplication(application.name)"> 
         <li><a href="#">{{application.name}}</a></li> 
        </ul> 
       </li> 
      </ul> 
     </div> 

myApp.controller("navbarController", function ($scope, $http) { 
    $scope.currentApplication = "default"; 

    var changeApplication = function(name) { 
     $scope.currentApplication = name; 
    }; 
    $http({ 
     method: 'GET', 
     url: 'applications' 
    }).then(function success(response) { 
     $scope.applications = response.data, 
    }); 
}); 

回答

0

ng-click="changeApplication什麼也不做,因爲你定義的函數爲:

var changeApplication = function(name) { 
    $scope.currentApplication = name; 
}; 

綁定到$scope as:

$scope.changeApplication = function(name) { 
    $scope.currentApplication = name; 
}; 
相關問題