动态加载后角度控制器无法正常工作

时间:2017-04-11 14:48:55

标签: javascript jquery angularjs

这是我试图动态加载的页面。我能够验证是否正在创建manolisApp对象,但控制器未运行。有没有我缺少的东西,或者它是否有动态加载的问题?

我正在通过ajax加载并插入容器的HTML页面:

<div class="row" ng-app="manolisApp">
    <div class="col-sm-3 col-md-2 sidebar">
        <ul class="nav nav-sidebar">
            <!--<li><a href="#">Reports</a></li>-->
        </ul>
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <div ng-controller="LVController" style="display: none;">
            <div class="lv-group" ng-repeat="group in groups" id="{{group.id}}">
                <div class="page-header"><h2>{{group.name}}</h2></div>
                <div class="lv-unit" ng-repeat="unit in group.units track by $index" id="{{unit.serial}}-{{unit.tank_num}}-{{unit.bin_num}}">
                    <p><strong>{{unit.alias}}</strong></p>
                    <div class="graphic" style="display: none;"></div>
                    <p class="small">Type: {{unit.dev_type}}</p>
                    <p class="small">Serial Number: {{unit.serial}}</p>
                    <div class="extra-info"></div>
                    <p class="small" style="clear: both;">{{unit.reading[0].date}}</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
var manolisApp = angular.module('manolisApp', []);
manolisApp.controller('LVController', function LVController($scope, $http) {
    var local_url = api_path+'/GetGroups/?username='+user;
    $http.get(local_url).then(function(GetGroups) {
        ...
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

使用

<script type="text/javascript">
var manolisApp = angular.module('manolisApp', []);
manolisApp.controller('LVController',['$scope','$http', function ($scope, $http) {
    var local_url = api_path+'/GetGroups/?username='+user;
    $http.get(local_url).then(function(GetGroups) {
        ...
    });
}]);
</script>

'$scope','$http'服务注入您的控制器。

同样,在function关键字后删除LVController。看看它是否有效