dongnaoben4456 2019-07-09 13:28
浏览 141
已采纳

在bootstrap模式中动态加载angularjs app?

I have a page, index.blade.php. (no angularjs)

<body>
    <table>
        <!-- If i click on a row, I store its id in a global variable (selected_id), 
              then I can click on show_edit_modal to view its details -->
        <tr></tr> 
        <tr></tr>
    </table>
    <button id="show_edit_modal">Show Modal</button>
    <div id="edit_modal">
      <!-- empty at first -->
    </div>
    $('#show_edit_modal').click(function(){
       // Fetch view
        $.ajax({
            type: 'GET',
            url: '/get_edit/' + selected_id,
            success: function(response) {
                $('#edit_modal').html(response);
            }
        }); 
    });
</body>

Click on “Add” button -> it will display a modal.

An ajax request will be sent on click to fetch the view (edit.blade.php) to be placed inside that modal. Not using an iframe. The request will return a view/blade file.

In the controller, receiver of ajax request:

$data = Model::find($id);

return view('edit', compact('data'));

edit.blade.php

<div ng-app="itemsModule">
    <div ng-controller="editController">
        ...
    </div>
</div>
<script src="angular.min.js">
<script src="/controllers/edit.js">

That fetched view has an angularjs app in it. The angularjs library script, controller script, all loaded inside that view.

Is this possible? If so, is there a standard way of doing it? Or should I try restructuring?

I have tried to run this and it works on the first click, but when I click on a different row and try to edit the details, I get a warning about attempting to load angularjs more than once.

  • 写回答

1条回答

  • dtp19819 2019-07-13 04:12
    关注

    I was able to solve this using AngularJS manual bootstrapping.

    I moved the angular.min.js to my index.blade.php.

    Then, in my edit.blade.php:

    <div id="helloApp">
        I am an angular app
        <div ng-controller="editController">
            Mighty controller
            <span ng-cloak>@{{message}}</span>
        </div>
    </div>
    <script>
        var ang = angular.module('itemsModule', []);
        ang.controller('editController', function($scope, $http){
            console.log('init edit controller');
            $scope.message = 'hello';
        });
    
        angular.element(function() {
            angular.bootstrap(document.getElementById('helloApp'), ['itemsModule']);
          });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。