search

Home  >  Q&A  >  body text

angular.js - angular route controller $http 请求两次

app.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/', {
        templateUrl: tplPath + '/posts.html',
        controller: 'mctrl'
    });
}]);

app.controller('mctrl', ['$scope', '$http', '$log', handleMctrl]);

function handleMctrl($scope, $http, $log) {
    var self = this;
    console.log(self);
    var resp = $http.get('/post/new');

    resp.success(function (data, status, headers, config) {
        $log.debug(data);
        self.data = data;
    });
};

会请求 /pot/new 两次

phpcn_u1582phpcn_u15822744 days ago494

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 16:59:18

    You mean:

    console.log(self)

    Printed twice?

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 16:59:18

    That’s probably because you referenced mctrl again on the page. Try removing ng-controller="mctrl"

    reply
    0
  • Cancelreply