AngularJS选择的项目不会重置为第一个/默认值

时间:2016-07-08 12:40:21

标签: javascript jquery angularjs

我的智慧结束了!在角度我有一个控制器和一个视图。 单击重启按钮后,页面上有2个下拉菜单需要重置为默认值。

我可以通过按下"选择"来设置框的值。选项进入控制器内的集合。但是,当按下重置按钮时,再次运行init()方法,应将下拉列表设置回第一个值。这不会发生,$ scope.selectedYear和$ scope.selectedReport的值保持不变,就像按下重置按钮之前一样。

这是控制器的完整代码

function TreeReportsCHLController($scope, $q, $routeParams, reportsDashboardResource, navigationService, notificationsService, dialogService, entriesManageDashboardResource, $timeout) {

    // Methods


    var generalError = function () {
        notificationsService.error("Ooops", "There was an error fetching the data");
        $scope.actionInProgress = false;
    }

    // Scope
    $scope.selectedYear = "";


    $scope.init = function () {

        $scope.hasCompleted = false;
        $scope.actionInProgress = false;
        $scope.yearSelected = false;
        $scope.reportTypes = ["Choose", "Entered", "ShortListed", "Winner", "Recieved"];
        $scope.selectedReport = "";
        $scope.entryYears = new Array();
        $scope.entryYears.push("Choose a Year");
        entriesManageDashboardResource.getEntryYears().then(function (response) {
            angular.forEach(response, function (value, key) {
                $scope.entryYears.push(value);
            });
        });

        $scope.selectedYear = $scope.entryYears[0];
            $scope.selectedReport = $scope.reportTypes[0];


    };

    $scope.yearHasSelected = function(selectedYear) {
        $scope.yearSelected = true;
        $scope.selectedYear = selectedYear;
    };


    $scope.generateFile = function (selectedReport) {
        $scope.actionInProgress = true;
        var reportDetail = {
            entryYear: $scope.selectedYear,
            chosenEntryStatus: selectedReport
        };

        reportsDashboardResource.generateEntriesReportDownloadLink(reportDetail).then(function (response) {
            if (response.Successful) {
                $scope.hasCompleted = true;
            } else {
                notificationsService.error("Ooops", response.ErrorMessage);
            }
            $scope.actionInProgress = false;
        }, generalError);
    };

    $scope.restart = function () {
        $scope.init();
    }



    // Initialise Page

    $scope.init();

}

angular.module("umbraco").controller("ReportsDashboardController", TreeReportsCHLController)

这是包含下拉列表的代码;

<table>
    <tr>
        <td>Get a report for year: {{selectedYear}}</td>
        <td><select ng-model="selectedYear" ng-change="yearHasSelected(selectedYear)" ng-options="year for year in entryYears" no-dirty-check></select></td>
    </tr>
    <tr ng-show="!hasCompleted && yearSelected">
        <td>
            Get Report Type:
        </td>
        <td>
            <select ng-model="selectedReport" ng-change="generateFile(selectedReport)" ng-options="status for status in reportTypes" no-dirty-check  ng-disabled="actionInProgress"></select>
        </td>
    </tr>
</table>

我还做了一个进一步的测试,我只是在重置方法中将$ scope.selectedYear设置为$ scope.entryYears [0]。当我在这里调试console.log $ scope.selectedYear时,该值确认它已被更改,但奇怪的是,我已将$ scope.selectedYear / {{selectedYear}}输出到页面进行测试,但这不会更新。它几乎就像控制器和视图之间的绑定没有发生一样。

任何帮助?

感谢,你。

1 个答案:

答案 0 :(得分:1)

这是一个有效的plunk,因为我无法访问您注入控制器的服务。我在控制器中所做的更改是:

首先,

$scope.entryYears = new Array();

变为

$scope.entryYears = [];

因为这是在js中声明数组的首选方法。

其次,我删除了包装的$ scope.apply()

$scope.selectedYear = $scope.entryYears[0];
$scope.selectedReport = $scope.reportTypes[0];

因为这导致了无限的消化周期。