好在我在我的项目中做到了这一点:
HTML
<select data-placeholder="Select an asset" class="input-xxlarge" ui-select2="sourceAssetId" ng-model="sourceAssetId" ng-options="asset.id as asset.name for asset in assets"></select>
指示
module.directive("uiSelect2", function() {
var linker = function(scope, element, attr) {
element.select2();
scope.$watch(attr.ngModel, function(newValue, oldValue) {
console.log("uiSelect", attr.ngModel, newValue, oldValue);
// Give the new options time to render
setTimeout(function() {
if(newValue) element.trigger("change");
})
});
}
return {
link: linker
}
});
相关控制器代码
$scope.$watch("sourceAssetId", function(newValue, oldValue) {
if(newValue) $scope.fetchAsset();
});