如何检查或调试AngularJS控制器是否正常工作?

时间:2019-07-10 08:12:57

标签: javascript angularjs

我想检查控制器是否在我的控制器上正常工作,我在创建控制器时尝试使用警报功能来测试我的控制器,但似乎什么也没发生。

JS

routerApp.controller('myCtrl', ["$scope", "$http", 
"$timeout", function($scope, $http, $timeout){
    $http({
        method: 'GET',
        url: 'image.json'
    }).then(function successCallback(response){
        $scope.images = response.data.items;
        console.log(response.data.items);

    $timeout(function(){
        $("#lightSlider").lightSlider({
            item:1,
            auto: true,
            loop:true,
            speed:1000,
            pause:3000,
        });
    },0);
    }, function errorCallback(response){
        alert("Something went wrong!");
    });
}]);

HTML

<div class="banner">
    <ul id="lightSlider">
        <li ng-repeat="image in images">
            <img ng-src="{{image.img}}"  />
        </li>
    </ul>
</div>

1 个答案:

答案 0 :(得分:0)

这些缺少ng-appng-controller, 看看下面的代码片段,就可以了

刚刚添加了ng-appng-controller

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="routerApp" ng-controller="myCtrl">
<div class="banner">
    <ul id="lightSlider">
        <li ng-repeat="image in images">
            <img ng-src="{{image.img}}"  />
        </li>
    </ul>
</div>
</div>

<script>
var app = angular.module('routerApp', []);
app.controller('myCtrl', function($scope) {


/**
* add your code here
*/
    alert('Debugger');
});
</script>

</body>
</html>