Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角材料$mdDialog-Can';t访问';这';确认功能中的项目_Javascript_Angularjs_Dialog_Angular Material - Fatal编程技术网

Javascript 角材料$mdDialog-Can';t访问';这';确认功能中的项目

Javascript 角材料$mdDialog-Can';t访问';这';确认功能中的项目,javascript,angularjs,dialog,angular-material,Javascript,Angularjs,Dialog,Angular Material,我试图使用angular material$mdDialog的confirm函数来清除数组,然后将该数组记录到控制台,但在$mdDialog函数本身中访问“this”对象/数组/表达式/函数时似乎存在问题,控制台会说,任何项引用都是未定义的,即使以前在其他控制器功能中使用 $mdDialog指令是否存在controllerAs语法问题 - 控制器: app.controller('notificationsController', function($scope, $state, $http,

我试图使用angular material$mdDialog的confirm函数来清除数组,然后将该数组记录到控制台,但在$mdDialog函数本身中访问“this”对象/数组/表达式/函数时似乎存在问题,控制台会说,任何项引用都是未定义的,即使以前在其他控制器功能中使用

$mdDialog指令是否存在controllerAs语法问题

-

控制器:

app.controller('notificationsController', function($scope, $state, $http, $document, $mdDialog, $filter, $timeout) {

this.selectedNotification = null;
this.notifications = [
    {
        title: 'Notification One',
        description: 'Description...',
        time: '2017-10-27T16:39:32+00:00',
        importance: 'Low',
        read: false
    },

    etc...

$scope.clearNotifications = function(ev) {
    var confirm = $mdDialog.confirm()
        .parent(angular.element('body'))
        .clickOutsideToClose(true)
        .title('Are you sure you want to clear all notifications?')
        .textContent('This action cannot be undone.')
        .ariaLabel('Confirm notifications list clearance')
        .ok('Yes')
        .cancel('No')
        .targetEvent(ev)

    $mdDialog.show(confirm).then(function() {
        $scope.status = 'All notifications deleted';
        console.log($scope.status);
        this.notifications.length = 0;
        console.log(this.notifications);
    }, function() {
        $scope.status = 'Notifications list not cleared';
        console.log($scope.status);
    })
}

位于:

$mdDialog.show(confirm).then(function() {
    ...
    this.notifications.length = 0; // <---- here
    ...
}, function() {
    ...
})

为什么要使用this.notifications或任何this.variable,在angular 1中,所有内容都应该放在$scope变量下,即使用$scope.notifications={}设置值并通过var x=$scope.notificationsErm获取值,即使我知道情况并非如此。互联网上到处都有人说这是一个糟糕的方法,文档表明controllerAs语法与“this”items的结合使用EA angular 1设计非常糟糕,但你应该采纳他们的官方建议,如果你有良好的实践,那么直接跳到angular 5或他们最近称之为angular 5的东西,或者,如果您仍然希望使用factory ControllerAs使用angular 1,并且自angular 1 Hanks以来,文档中已建议使用“this”语法,请访问此链接。完美地工作
app.controller('notificationsController', function($scope, $state,           
$http, $document, $mdDialog, $filter, $timeout) {

  var _this = this; // <--- Now _this is the controller
  this.notifications = [
  {
    title: 'Notification One',
    description: 'Description...',
    time: '2017-10-27T16:39:32+00:00',
    importance: 'Low',
    read: false
  },

etc...

$scope.clearNotifications = function(ev) {
  ...

  $mdDialog.show(confirm).then(function() {
    ...
    _this.notifications.length = 0; //<--- using _this and not this
    ...
  }, function() {
    ...
  })
}