【问题标题】:AngularJS Material mdDialog and display locals in a templateAngularJS 材质 mdDialog 并在模板中显示局部变量
【发布时间】:2018-01-26 06:59:50
【问题描述】:

产生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 }}

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

【问题讨论】:

    标签: javascript angularjs angularjs-controlleras angularjs-material


    【解决方案1】:

    由于您使用 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。

    【讨论】:

    • 刚刚发现我不能重用为另一个对话框定义的DialogController,因为我需要将item传递给它。
    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2014-07-20
    • 2018-06-17
    • 2015-04-11
    相关资源
    最近更新 更多