【问题标题】:AngularJS calling array with functionAngularJS用函数调用数组
【发布时间】:2015-04-13 13:21:20
【问题描述】:

我正在尝试使用 angularjs 创建一个上下文菜单。当用户单击右键时,上下文菜单将打开,但是上下文菜单可能会根据用户级别而变化,因此我想用函数调用“mycontext”菜单数组,但它确实当我用角度控制器中的函数调用它时不起作用。

我想用'launch()'函数调用我的上下文菜单。

<a ng-context-menu="launch(y)">Open Context Menu</a>

我的控制器:

controller: function($scope) {

  $scope.launch = function (m) {

    // .......................

    $scope.mycontext = [
      [
        'opt1',
        function () {
          console.log("buy");
        }
      ], [
        'opt2',
        function () {
          console.log("sell");
        }
      ]
    ];

  };

}

如何使用调用函数运行它?

【问题讨论】:

标签: javascript angularjs model-view-controller


【解决方案1】:

我认为您想要一个包含 JSON 对象的数组。

var test = [
      {
        someValue:'tada',
        someFunct: function() {
         alert('tada!');   
        }
      },
      {
        someValue:'something else',
        someFunct: function() {
          alert('something else here');
        }
     }];

我用简单的 jQuery 做了一个例子,但它应该很容易被你的 Angular 代码采用。 http://jsfiddle.net/fan4Lwu4/

【讨论】:

    【解决方案2】:

    看起来你想调用$scope.mycontext数组里面的函数,你可以写一个循环调用函数。

    angular
      .module('demo', [])
      .controller('DefaultController', DefaultController);
      
      function DefaultController() {
        var vm = this;
        vm.run = run;
        
        function run() {
          vm.mycontext = [
          [
          	'opt1',
            function () {
              console.log("buy");
            }
          ], 
          [
            'opt2',
            function () {
              console.log("sell");
            }
          ]
        ];
        
        for (var i = 0; i < vm.mycontext.length; i++) {
        	vm.mycontext[i][1]();
        }
        }
      }
      
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="demo">
      <div ng-controller="DefaultController as ctrl">
        <button type="button" ng-click="ctrl.run()">Run</button>
      </div>
    </div>

    如果您可以更改数组本身,也可以使用立即调用函数表达式。

    angular
      .module('demo', [])
      .controller('DefaultController', DefaultController);
      
      function DefaultController() {
        var vm = this;
        vm.run = run;
        
        function run() {
          vm.mycontext = [
          [
          	'opt1',
            (function () {
              console.log("buy");
            })()
          ], 
          [
            'opt2',
            (function () {
              console.log("sell");
            })()
          ]
        ];
        }
      }
      
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="demo">
      <div ng-controller="DefaultController as ctrl">
        <button type="button" ng-click="ctrl.run()">Run</button>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2018-02-19
      • 2019-06-30
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多