當頁面加載時,下拉列表與某些值綁定(比如1,2 ... 7) 基於一個條件,我想設置它的選定值。在承諾中設置時,它不起作用。如何解決這個問題。保留在promise中獲得的值 - angular
HTML:
<div ng-app="app" ng-controller="Ctrl">
<select id="ddlSets" data-ng-model="SetId">
<option value="0">-- Select Set --</option>
<option data-ng-repeat="item in Sets" value="{{item.Id}}">{{item.Code}}</option>
</select>
</div>
ctrl.js:
var app = angular.module("app",[]);
app.controller("ctrl",["$scope","Factory",function($scope,Factory){
var init = function(){
//$scope.Sets = (code here to bind data to the dropdown)
var promise = Factory.GetSetId();
promise.then(function (success) {
if (success.data == 5) {
$scope.SetId = 5; //value isn't set. It remains 0.
}
},
function (error) {
console.log(error);
});
}
//it works if set here - $scope.SetId = 7;
init();
}]);
factory.js:
app.factory("Factory", ["$http",function ($http) {
var factory = {};
factory.GetSetId = function() {
return $http({
url: "http://localhost:12345.asmx/GetSetId",
method: "GET"
}).success(function (data, status) {
}).error(function (data, status) {
});
}
return factory;
}]);
你可以做一個plunker顯示這種行爲? –
不知道如何做到承諾/ $ http。如果我只是用代碼 – Qwerty
創建一個,如果它實際上沒有證明問題,那麼它會沒問題,那麼沒有多大意義!如果你被困住了,也許一個單獨的SO問題可以幫助你解決問題。 –