-->

Dynamically generate modals with MobileAngularUI

2019-08-04 19:01发布

问题:

I am using the Mobile Angular UI framework for my mobile web-app.

This Page explains the directive on how to generate modals (they slightly change the bootstrap framework)

I manage to reproduce the example, however when I wrap the directive in a ng-repeat to generate multiple modals, it is not working any more

  <div ui-content-for="modals">
    <div   ng-repeat="p in UseCaseCtrl.getProjects()" >
      <div class="modal" ui-if="modal{{ p.getId() }}" ui-state="modal{{ p.getId() }}">
        <div class="modal-backdrop in"></div>
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button class="close" 
                      ui-turn-off="modal{{ p.getId() }}">&times;</button>
              <h4 class="modal-title">{{ p.getName() }}</h4>
            </div>
            <div class="modal-body">

                ...Lorem...

            </div>
            <div class="modal-footer">
              <button ui-turn-off="modal{{ p.getId() }}" class="btn btn-default">Close</button>
              <button ui-turn-off="modal{{ p.getId() }}" class="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>        

As far as I experienced, the problem seems to come from ui-state="modal{{ p.getId() }}, as it works again when I hardcode only that one (i.e., set ui-state="modal1")

Am I missing something? Any workaround to generate modals with ng-repeat?

Thanks

回答1:

I use ng-click = "functionName(p.getId()) in controller instead of ui-turn-on="modal{{ p.getId() }}.

Details:

$scope.functionName = function (id) {
    SharedState.initialize($rootScope, 'modal'+id); 
    $rootScope.Ui.turnOn( 'modal'+id);
}; 

note: you need injection SharedState in controller.

I done it and success!