2017-10-24 2 views
0

Ich möchte ein Array zu einem modalen Dialog mit einer Vorlage übergeben.Dialog nicht richtig funktioniert nach dem zweiten Aufruf

Da ich AngularJS nur für heute verwendet habe, stammt 99% des Codes aus dem Dokument.

Mein aktueller Code, der zum ersten Mal arbeitet den Dialog zu öffnen, aber nicht nach: (die md-Option ist leer und auch die Tasten (in der Nähe, bestätigt) funktionieren nicht)

angular.module("materialExample").controller("calendarCtrl", function ($scope, $filter, $q, $timeout, $log, $mdDialog, MaterialCalendarData) { 

$scope.showAdvanced = function(ev) { 

$scope.data= ev; 

$mdDialog.show({ 
    controller: DialogController, 
    controllerAs: 'ctrl', 
    templateUrl: 'includes/dialog.tmpl.html', 
    parent: angular.element(document.body), 
    clickOutsideToClose:true, 
    fullscreen: $scope.customFullscreen ,// Only for -xs, -sm breakpoints. 
    scope: $scope, 
    resolve: { 
     test: function() { 
      return $scope.data; 
     } 
    } 
}) 
    .then(function(answer) { 
     $scope.status = 'You said the information was "' + answer + '".'; 
    }, function() { 
     $scope.status = 'You cancelled the dialog.'; 
    }); 
}; 

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

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

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

Und in Die Vorlage dialog.tmpl.html

Ist dies der richtige Weg, um eine Variable zu übergeben? Wie behebe ich den Code, damit der DialogController für aufeinanderfolgende geöffnete Dialoge funktioniert?

Entfernen

scope: $scope, 

fixiert das DialogController aber ich kann nicht die Daten übergeben.

Antwort

1

Die $ mdDialog kann mit einem Einheimischen Attribut aufgerufen werden (verwendet ein dict), in dem Sie geben params:

$mdDialog.show({ 
controller: DialogController, 
controllerAs: 'ctrl', 
templateUrl: 'includes/dialog.tmpl.html', 
parent: angular.element(document.body), 
clickOutsideToClose:true, 
fullscreen: $scope.customFullscreen ,// Only for -xs, -sm breakpoints. 
scope: $scope, 
**locals: {element:$scope.element}**, 
resolve: { 
    test: function() { 
     return $scope.data; 
    } 
} 

})

und dann Controller:

(function() { 
'use strict'; 

angular 
    .module('myapp') 
    .controller('mycontroller', mycontroller); 

/* @ngInject */ 
function mycontroller($scope, $mdDialog, element) { 
    $scope.element = element // now you can use $scope.element in this controller and it will work :) 
    // what ever you need to do here 
... 
} 
})() 
+1

Danke! Die Suche nach Einheimischen hat mir sehr geholfen. Jetzt funktioniert es ohne den Umfang und die Lösung. – Beig

Verwandte Themen