我正在编写一些用于添加和编辑资源的表单。我正在使用 Angucomplete-alt ( https://github.com/ghiden/angucomplete-alt ) 和我自己的下拉指令从数据库中获取选择。这些与主窗体位于单独的控制器中。我的常规文本字段在编辑表单上填充得很好,但是我在使用 Angucomplete 和选择时遇到了问题。范围数据在页面加载时存在。我编写了一个函数来使用 URL 中的 ID 来获取它。但除非您重新加载页面,否则它们并不总是填充。我怎样才能让他们每次都填充?
这是我填充表单的函数:
$scope.popForm = function(clientId) {
var config = {
params: {clientId: $stateParams.clientId},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
$http.get('assets/php/clients/pop_client.php', config)
.then(function(data) {
var realStatus = data.data.status;
if(realStatus == 'success'){
//Set the $scope from the JSON response
$scope.client = data.data.client;
//Broadcast a bunch of events with data to the dropdowns
$timeout(function() {
$rootScope.$broadcast('setTypeDropdown', data.data.client.type);
$rootScope.$broadcast('setCatDropdown', data.data.client.category);
$rootScope.$broadcast('setTeamDropdown', data.data.client.team);
$rootScope.$broadcast('setA1Dropdown', $scope.client.assigned1);
$rootScope.$broadcast('setA2Dropdown', $scope.client.assigned2);
});
}
});
};
我有另一个控制器负责选择,这就是为什么将数据广播到它们的隔离范围的原因。这是 $on 函数之一,因为它们基本相同。(必须有一种不那么复杂的方式来做到这一点......)
// Options are the select options that I get from the database
// for each instance of the select controller
$scope.$on('setTypeDropdown', function(event, type) {
var i = 0;
$.each($scope.options, function(){
if (this.value == type){
$scope.client.type = $scope.options[i];
}
i++;
});
});
那么,有没有更好的方法来做到这一点?因为这根本行不通...
编辑: Angucomplete-alts 现在运行良好。只是我令人费解的选择要担心。想知道对它们进行去角化而不使用我的指令是否会更好。