1

生成的控制器的一部分mdDialog看起来像

$scope.removeAttendee = function(item) {

    console.log(item);

    $mdDialog.show({
        controller: DialogController,
        templateUrl: 'views/removeMsg.tmpl.html',
        parent: angular.element(document.body),
        clickOutsideToClose:true,
        controllerAs: 'ctrl',
        fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
        locals: {item: item}
    })

和控制器mdDialog

function DialogController($scope, $mdDialog) {

    var attendee = this;

    $scope.hide = function() {
        $mdDialog.hide();
    };

    $scope.cancel = function() {
        $mdDialog.cancel();
    };

    $scope.save = function(response) {
        $mdDialog.hide(response);
    };

    $scope.remove = function(response) {
        $mdDialog.hide(response);
    };

}

removeMsg.tmpl.html有那个代码

<p>You are going to remove {{ ctrl.item.firstName }} {{ ctrl.item.lastName }} from the lunch list.</p>

但即使我将代码更改为简单的东西,我也无法显示相关值

locals { item: "test" }

和相关部分

{{ ctrl.item }}

有什么建议为什么我没有显示这些值?

4

1 回答 1

2

由于您使用DialogController的是controllerAs别名,因此您应该将解析的值分配给控制器上下文item变量

function DialogController($scope,$mdDialog,item){//Item value will resolve via locals
    var attendee = this;
    attendee.item = item; // this line is important.

    $scope.hide = function() {
        $mdDialog.hide();
    };

    $scope.cancel = function() {
        $mdDialog.cancel();
    };

    $scope.save = function(response) {
        $mdDialog.hide(response);
    };

    $scope.remove = function(response) {
        $mdDialog.hide(response);
    };
}

除此之外,请转换$scope方法使用attendee(this)。由于controllerAs模式不鼓励$scope在控制器中使用。

于 2017-08-17T18:34:58.123 回答