ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-为什么在模态解析中声明一个函数

2019-11-21 18:38:42  阅读:288  来源: 互联网

标签:bootstrap-modal angularjs angular-ui javascript function


关于模态angular-ui的此代码示例,我有2个简单的问题.

可以找到代码here,我还引用了有趣的部分:

var ModalDemoCtrl = function ($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function (size) {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
};

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function () {
    $modalInstance.close($scope.selected.item);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
};

所以我的第一个问题是,为什么我需要在项目上声明一个函数:

resolve: {
  items: function () {
    return $scope.items;
  }
}

我不能做这样的事情:

resolve: {
  items: $scope.items;
}

我的第二个问题是为什么项目不是单引号的字符串? javascript如何不将键混淆为局部变量?

谢谢您的解释!

解决方法:

对于第一个问题:

resolve: {
  items: function () {
    return $scope.items;
  }
}

resolve.items是一个函数.
resolve.items()返回一个数组

resolve: {
  items: $scope.items;
}

resolve.items是一个数组
resolve.items()导致错误.

需求是因为框架希望调用一个函数.

对于第二个问题:JS!= JSON.语法使之成为可能.如果您编写了items = …而不是items:…,那么它将被创建为全局变量(或者,如果严格模式处于活动状态,则会引发错误)

标签:bootstrap-modal,angularjs,angular-ui,javascript,function
来源: https://codeday.me/bug/20191121/2053692.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有