我一直在使用Angular Material,我想创建一个$ mdDialog,允许用户输入信息,保存后,将更新绑定到ng-repeat的对象.
在试图让这个工作并尝试mdDialog.show()的不同参数时,我很困惑什么时候/为什么使用范围.
这是第一个实现:
(function () {'use strict';
angular.
module('myApp', ['ngMaterial']).
controller('AppCtrl', AppCtrl);
function AppCtrl($mdDialog, $scope) {
$scope.lister = [{name:'Matt'},{name:'Steve'}];
$scope.showDialog = showDialog;
function showDialog(evt) {
$scope.obj = {name:'default'};
$mdDialog.show({
targetEvent: evt,
scope: $scope.$new(),
template:
'<md-dialog>' +
' <md-content><md-input-container>'+
' <label>Name</label>'+
' <input ng-model="obj.name">' +
' </md-input-container></md-content>' +
' <div class="md-actions">' +
' <md-button ng-click="close(obj)">' +
' Save' +
' </md-button>' +
' </div>' +
'</md-dialog>'
}).then(function(objs){$scope.lister.unshift(objs)});
}
$scope.close = function(objs){
$mdDialog.hide(objs);
}
}
}());
Run Code Online (Sandbox Code Playgroud)
上面代码的行为是mdDialog将在Name输入字段中以"default"打开,但如果我将show()参数更改为下面(键差异是交换"scope:"为"controller:"):
function showDialog(evt) { …Run Code Online (Sandbox Code Playgroud)