跟進從AngularJS : Why my watch is not working使用AngularJS看得到下拉的所選項目不工作
function myController($scope, $http) {
$scope.abc = abcValueFromOutsideOfMyController;
$scope.getAbcCnt= function()
{
url2 = baseURL + '/count/' + $scope.abc;
$http.get(url2).success(function (data) {
$scope.abcCnt = data.trim();
});
};
$scope.$watch('abc',getAbcCnt);
}
爲什麼我得到$ scope.abc未定義的錯誤。
abcValueFromOutsideOfMyController通過下拉菜單選擇元素進行設置。它最初是未定義的,但一旦選擇了下拉菜單,該值將是非定義的,我在控制檯中驗證,我得到了一些值。
降下來代碼是在這裏
$(document).on('click', '.dropdown-menu li a', function() {
selectedItem = $(this).text();
$("#selectedItem").text(selectedItem);
abcValueFromOutsideOfMyController= selectedItem.trim();
console.log(abcValueFromOutsideOfMyController);
});
這裏是我的html
<div class="dropdown btn">
<div id="collectiondropDown" class="dropdown-toggle" id="dLabel" role="button" data-toggle="dropdown"
data-target="#" >
<span id="selectedItem" ng-model="abc">Items</span>
<b class="caret"></b>
</div>
<ul id="dropDownUL" class="dropdown-menu pull-left" role="menu" aria-labelledby="dLabel">
</ul>
</div>
我是新來AngularJS,讓我知道如果我錯過了一些基本的概念,上面是不可能的去做。
+1。感謝您的信息。我剛剛添加了我的html下拉代碼,我也嘗試在幾個地方添加ng模型,如你推薦的,我仍然得到$ scope.abc undefined。僅供參考,我用twitter bootstrap的dropddown – Watt 2013-05-10 17:02:05
我已經更新了我的答案。 – 2013-05-10 17:48:57
謝謝先生!現在就試試這個。 – Watt 2013-05-10 19:40:51