How to use javascript inside a page with type=“text/ng-template”?

前端 未结 3 866
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 16:39

I have code like this:



        
相关标签:
3条回答
  • 2021-01-26 17:21

    You can better add the following table-html to your template like this:

    <table>
      <thead>
        <tr>
          <th>菜名</th><th>单价</th><th>份数</th><th>小计</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="id in obj">
           <td>{{name}}</td>
           <td>{{price}}</td>
           <td>{{num}}</td>
           <td>{{total}}</td>
        </tr>
      </tbody>
    </table>
    

    Ng-Repeat will render the necesarry table rows for every item in your obj variable. After that you will need to define $scope.obj in your controller since your angular app will look for that. I think that just definig var obj is not working, but I have never used it that way.

    0 讨论(0)
  • 2021-01-26 17:21

    This won't work. Instead just use Angular ng-repeat inside the template to generate your table.

    0 讨论(0)
  • 2021-01-26 17:23

    I assumed you are using the ui-bootstrap-modal (text/ng-template) thus you are able to pass the object you want to use into the controller of your modal by using the resolve functionality.

    var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'xml-feed.html',
                controller: function($modalInstance,  detail) { 
                    this.detail = detail;
    
                    this.close= function () {
                        $modalInstance.close();
                    }
                },
                controllerAs: 'myModal',
                size: 'lg',
                resolve: {
                    detail: function () {
                        //here you will need to return a reference
                        //I assumed the modal is opened in a controller
                        //that already has the detail object on its scope
                       return $scope.detail;
                    }
                }
    
            });
    

    And in your modals HTML you can simple use the object in an angular way.

    <table>
      <thead>
        <tr>
          <th>菜名</th><th>单价</th><th>份数</th><th>小计</th>
        </tr>
      </thead>
      <tbody>
            <tr><td>{{myModal.detail}}</td><tr> //do what you need with this object
      </tbody>
    </table>
    

    P.S. I used the controllerAs syntax in the modal.

    0 讨论(0)
提交回复
热议问题