指令's的代码在我得到数据之前就启动了

Directive's code fires before I got data

本文关键字:数据 启动 代码 指令      更新时间:2023-09-26

这是这个问题的延续从选择ng选项中获取其他列

我的指令有问题。第一个问题是,在表单的第一次初始加载中,我可以正确地看到部门和类别,但项目显示的是"选择项目"而不是项目。第二个问题是,当我导航到列表中的另一行时,初始值没有显示(例如,所有显示的都是"选择部门"、"选择类别"、"选定项目",而不是值。我确认在这种情况下,我还没有检索到该行的数据。因此,只有在获得数据后,我才需要运行指令的代码。

我该如何解决我的问题?

以下是指令的完整代码:

(function () {
    'use strict';
    var app = angular.module('sysMgrApp');
    app.directive('smDci', ['departmentService', 'categoryService', 'itemService', smDci]);
    function smDci(departmentService, categoryService, itemService) {
        var directive = {
            restrict: 'E',
            scope: {
                selectedDepartmentId: '=?departmentid',
                selectedCategoryId: '=?categoryid',
                selectedItemId: '=itemid',
                selectedDci:  '=?dci'
            },
            controller: ['$scope', 'departmentService', 'categoryService', 'itemService',
                function ($scope, departmentService, categoryService, itemService) {
                $scope.selectedDepartmentId = $scope.selectedDepartmentId || 0;
                $scope.selectedCategoryId = $scope.selectedCategoryId || 0;
                $scope.selectedItemId = $scope.selectedItemId || 0;
                $scope.selectedDci = $scope.selectedDci || '';                
               
                var init = function () {
                    $scope.metaData = {};
                    window.console && console.log('Selected departmentId = ' + $scope.selectedDepartmentId +
                        ' Selected categoryId = ' + $scope.selectedCategoryId + ' Selected ItemId = ' + $scope.selectedItemId);
                    departmentService.getAllDepartments().then(function (data) {
                        $scope.metaData.departments = data.departments;
                        //window.console && console.log('Got all departments...')
                    });
                    if ($scope.selectedDepartmentId == 0 && $scope.selectedCategoryId == 0 && $scope.selectedItemId != 0) {
                        itemService.getItemById($scope.selectedItemId).then(function (data) {
                            var item = data.item;
                            if (item != null) {
                                $scope.selectedItem = item;
                                $scope.selectedDepartmentId = item.departmeId;
                                $scope.selectedCategoryId = item.categoryId;
                                
                                window.console && console.log('Initial selections are about to fire...')
                                getInitialSelections();
                            }
                        });
                    }
                    else {
                        getInitialSelections();
                    }
                };
                var getInitialSelections = function()
                {
                    if ($scope.selectedDepartmentId != 0) {
                        $scope.departmentChanged($scope.selectedDepartmentId);
                    }
                    if ($scope.selectedCategoryId != 0) {
                        $scope.categoryChanged($scope.selectedCategoryId);
                    }
                }
                $scope.departmentChanged = function (departmentId) {
                    if (!departmentId) {
                        $scope.selectedCategoryId = '';
                        $scope.selectedItemId = '';
                        $scop.selectedItem = {};
                        $scope.selectedDci = '';
                    }
                    else
                    {
                        categoryService.getCategoriesByDepartmentId(departmentId).then(function (data) {
                            $scope.metaData.categories = data.categories;
                            //window.console && console.dir($scope.selectedItem);
                        });
                    }
                };
                $scope.categoryChanged = function (categoryId) {
                    if (!categoryId) {
                        $scope.selectedItemId = '';
                        $scope.selectedItem = null;
                        $scope.selectedDci = '';
                    }
                    else
                    {
                        itemService.getItemsByCategoryId(categoryId).then(function (data) {
                            $scope.metaData.items = data.items;
                        });
                    }
                };
                $scope.itemChanged = function(item)
                {
                    $scope.selectedItemId = item.itemId;
                    $scope.selectedDci = item.department + item.category + item.item;
                }
                init();
            }],
            templateUrl: 'app/templates/smDci'
        };
        return directive;
    }
})();

及其HTML:

<div class="row">
    <label class="control-label col-md-2" title="@Labels.dci">@Labels.dci:</label>
   
    <div class="col-md-3">
        <select class="form-control" ng-model="selectedDepartmentId" name="department" id="department"
                ng-options="d.departmeId as d.descrip for d in metaData.departments"
                data-no:dirty-check
                ng-change="departmentChanged(selectedDepartmentId)">
            <option value="">@String.Format(Labels.selectX, Labels.department)</option>
        </select>
    </div>
    <div class="col-md-3">
        <select class="col-md-3 form-control" ng-model="selectedCategoryId" id="category" name="category"
                ng-disabled="!selectedDepartmentId"
                data-no:dirty-check
                ng-change="categoryChanged(selectedCategoryId)"
                ng-options="c.categoryId as c.descrip for c in metaData.categories | filter: {departmeId:selectedDepartmentId}">
            <option value="">@String.Format(Labels.selectX, Labels.category)</option>
        </select>
    </div>
    <div class="col-md-3">
        <select class="col-md-3 form-control" ng-model="selectedItem" id="item" name="item"
                ng-disabled="!selectedCategoryId"
                ng-change="itemChanged(selectedItem)"
                ng-options="c as c.descrip for c in metaData.items | filter: {departmeId:selectedDepartmentId, categoryId:selectedCategoryId}">
            <option value="">@String.Format(Labels.selectX, Labels.item)</option>
        </select>
        <div class="field-validation-error">
            <span ng-show="item.$error.required">@String.Format(Messages.isRequired, Labels.item)</span>
        </div>
    </div>
</div>
<div class="clearfix"></div>

这就是我如何使用它的形式:

<data-sm:dci itemid="currentCardAct.itemId"  dci="currentCardAct.dci"></data-sm:dci>

使用登录到控制台,我可以看到在我需要之后检索到的卡数据,例如在控制台中我看到

选定部门Id=0选定类别Id=0所选项目Id=0js:221检索到当前卡活动数据。。smDci.js:28选定部门Id=0选定类别Id=0所选项目Id=0js:221检索到当前卡活动数据。。

我想我可以在指令的代码中添加手表,但这是唯一的选择吗?

我通过添加手表解决了这个问题。我现在正试图解决项目的初始选择问题,显然这是一个已知的问题,如本文所述http://odetocode.com/blogs/scott/archive/2013/06/19/using-ngoptions-in-angularjs.aspx关于初始选择。