绑定模型的更改未反映在控制器中 - Angularjs指令

时间:2016-08-31 05:58:57

标签: javascript angularjs

我已经编写了一个指令来创建角度JS中的自定义下拉列表。

这就是我在UI上呈现它的方式:

<cd-dropdown class="col-xs-12 col-md-5 col-lg-5" id-property="id" value-property="name" placeholder="Select City" on-select-change="onCityChangeSetFirstRegionAsDefault" options="cities_array" selected="city.selected" selection-type="single"></cd-dropdown>

它工作正常,但是,我试图让所选值反映在呈现指令的视图的控制器中。让我来说明一下 -

GetLocationDetails.html

<cd-dropdown id-property="id" value-property="name" placeholder="Select City" on-select-change="updateCity" options="locationInformation.arrayOfCities" selected="locationInformation.selectedCity" selection-type="single"></cd-dropdown>

GetLocationDetailsCtrl.js

$scope.updateCity = function () {
    console.log($scope.locationInformation.selectedCity); //This is logging the previously selected city (undefined if this is the first selection).
    $scope.locationInformation.selectedRegion = undefined;
};

指令 CdDropdown.js

careDimensionApplication.directive('cdDropdown', function() {
    return {
        restrict: 'E',
        scope: {
            options: '=',
            selected: '=',
            onSelectChange: '='
        },
        templateUrl: 'views/common/cd-dropdown.html',
        controller: 'cdDropdownController',
        link: function (scope, element, attrs) {
            scope.placeholder = attrs.placeholder;
            scope.selectionType = attrs.selectionType;
            scope.valueProperty = attrs.valueProperty;
            scope.idProperty = attrs.idProperty;
            element.on('click', function () {
                scope.bindOptions(scope.options);
            });
        }
    };
}); 

我的指令 CdDropdownCtrl.js

的控制器

(这是在下拉列表中更改选择时调用的函数)

function (selectedItem) {
    popup.dismiss('ok');
    $scope.isValueSelected = selectedItem != undefined;
    if (($scope.selected == undefined && selectedItem != undefined) || (!($scope.selected == undefined && selectedItem == undefined) && $scope.selected[$scope.idProperty] != selectedItem[$scope.idProperty])) {
        $scope.selected = selectedItem;
        if ($scope.onSelectionChanged != undefined)
            $scope.onSelectionChanged();
    }
};

问题在于,传递给指令的onSelectionChanged()函数(在上面的示例中为updateCity)打印以前选择的城市。如何在那里获得当前选择的选项?

0 个答案:

没有答案