AngularJS材质mdDialog并在模板中显示本地 [英] AngularJS Material mdDialog and display locals in a template

查看:68
本文介绍了AngularJS材质mdDialog并在模板中显示本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Part of the controller which spawn mdDialog looks like

$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>

但是即使我将代码更改为类似

But I'm not able to display related values even when I will change code to something simple like

locals { item: "test" }

及相关部分

{{ ctrl.item }}

我为什么不显示这些值的任何建议?

Any suggestions why I'm not getting display those values?

推荐答案

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

Since you're using DialogController with controllerAs alias, you should assign resolved value to controller context item variable

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 方法以使用与会者(本).由于 controllerAs 模式不鼓励在控制器中使用 $ scope .

Apart from this, please shift $scope methods to use attendee(this). As controllerAs pattern doesn't encourage to use $scope in controller.

这篇关于AngularJS材质mdDialog并在模板中显示本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆