Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

javascript - 试图弄清楚为什么console.log被执行两次[重复](Trying to figure out why does console.log gets executed twice [duplicate])

Why does the console.log execute twice even though I am only calling it once.

(为什么console.log执行两次,即使我只调用一次。)

You can see the console.log by inspecting the console on this page.

(您可以通过在页面上检查控制台来查看console.log。)

I am using angularjs to retrieve the data that I am trying to console.

(我正在使用angularjs检索要尝试控制台的数据。)

I couldn't find any online resources pointing this being an issue with angularjs Below is the js but it is also available when viewing source on the page.

(我找不到任何在线资源,指出这是angularjs的问题。下面是js,但在查看页面上的源代码时也可用。)

JS

(JS)

var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $http, $filter) {
    $http.get("files/tickets.json")
    .then(function (response) {
        var status = response.status;
        var statusText = response.statusText;
        var headers = response.headers;
        var config = response.config;

        $scope.tickets = response.data;

        $scope.ticketsA = $scope.tickets["ticketsA"];
        $scope.ticketsB = $scope.tickets["ticketsB"];

        console.log($scope.tickets);
        console.log("Testing...");

        ...

    }).catch(function (response) {
        console.log(response);
    })
});
  ask by Jigoro Kano translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is executing twice because the ng-controller is instantiated twice:

(它执行两次,因为ng-controller被实例化了两次:)

<body ng-app="myApp" ng-controller="myController" class="ng-scope">
    <div class="wrapper" style="margin-top: 20px;">
        <div class="container ng-scope" ng-app="myApp" ng-controller="myController">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...