在 angular js 中向控制器注入常量(模式弹出窗口问题)

Inject constant to a Controller in angular js(Issue with modal popup)

我在 ng grid.So 中连续单击一个按钮显示一个模式弹出窗口,以在模式弹出窗口中获取一个表单,我正在用 JSON 模式填充它

Modal Pop Up的Html是

<div class="modal-header">
      <h3 class="modal-title">Edit Row</h3>
  </div>
  <div class="modal-body">
    <form sf-schema="schema" sf-form="form" sf-model="entity"></form>
  </div>
  <div class="modal-footer">
      <button class="btn btn-success" ng-click="save()">Save</button>
      <button class="btn btn-warning" ng-click="$close()">Cancel</button>
  </div>
</div>

架构在外部 js 文件中被放入常量中,如下所示:

var app = angular.module("myApp", ['ngGrid','ui.bootstrap']);
app.constant('UserPopUpSchema', {
    type: 'object',
    properties: {
        FullName: { type: 'string', title: 'FullName' },
        UserName: { type: 'string', title: 'UserName' },
        Password: { type: 'string', title: 'Password' },
        EmailAddress: { type: 'string', title: 'EmailId' },
        Phone: {type:'string',title:'phNum'}
    }
});

所以单击 ng-grid 中的一行时,我正在执行一个名为 edit row 的函数,它将打开弹出窗口并根据常量填写表格。

 $scope.editRow = function (grid,row) {
        $modal.open({
            templateUrl: 'PopUpTemplate.htm',
            controller:['$modalInstance', 'grid', 'row','UserPopUpSchema', function($modalInstance,grid,row){

        schema = 'UserPopUpSchema';

         entity = angular.copy(row.entity);
         form = [
           'FullName',
           'UserName',
           'Password',
           'EmailId',
          'phNum'
           ];


            }],
            resolve: {
                grid: function () { return grid; },
                row: function () { return row; }
            }
        });
    };

但是在这里我对如何向控制器函数注入常量有点困惑。结果模态以空白形式出现。

感谢任何帮助!!

试试这个

$modal.open({
    templateUrl: 'PopUpTemplate.htm',
    controller: function ($modalInstance, grid, row, UserPopUpSchema) {
     // Do Stuff
    },
    resolve: {
        grid: function () {
            return grid;
        },
        row: function () {
            return row;
        },
        UserPopUpSchema: function () {
            return UserPopUpSchema;
        }
    }
});
 app.controller('Myctrl',function($scope,UserPopUpSchema) {
    $scope.editRow = function (grid,row) {
        $modal.open({
        templateUrl: 'PopUpTemplate.htm',
        controller:['$modalInstance', 'grid', 'row','UserPopUpSchema', function($modalInstance,grid,row){

    schema = 'UserPopUpSchema';

     entity = angular.copy(row.entity);
     form = [
       'FullName',
       'UserName',
       'Password',
       'EmailId',
      'phNum'
       ];


        }],
        resolve: {
            grid: function () { return grid; },
            row: function () { return row; }
            UserPopUpSchema: function () { return UserPopUpSchema; }
        }
    });
};
 });

 and in the modal instance controller inject the UserPopUpSchema along with grid and row

$modal.open() 接受一个 "resolve" 属性,你可以在其中传递你想要的所有数据。

$modal.open({
    templateUrl: 'templateUrl',
    controller: 'controllerName',
    resolve: {
        data: function(){
            // Here, return a JSON object containing all the data you want to pass to the controller.  
        }
    }
});

那么您的控制器将如下所示:

yourApp.controller('controllerName', function ( $scope, $modalInstance, data ){
    // Here, using data, you can access the JSON object you passed previously.
});