0
我有一個包含一個XHR功能的主控制器,這樣嵌套控制器和功能 - 返回值不確定
app.controller('mainCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.xhr = function(requestData) {
var ajax = "/ajax/";
$http({ url: ajax, data: requestData, method: 'POST' })
.success(function(data) {
console.log(data); // data shows up in the console,, but after the undefined from directive controller
return data;
})
.error(function(err){"ERR", console.log(err)});
}
}]);
然後我有一個使用來自於它自己的控制器,主控制器XHR功能的指令,,像這樣
app.directive("register", function ($http) {
return {
restrict: "E",
controller: function ($scope, $element, $attrs) {
$scope.register = {};
$scope.request = {};
$scope.response = {};
$scope.createAccount = function() {
$scope.request= {
action: "register",
data: $scope.register
}
$scope.response = $scope.xhr($scope.request);
console.log($scope.response);
}
},
templateUrl: "register.html"
}
});
當我觸發$ scope.createAccount功能指令的控制器,則返回值是不確定的。我無法弄清楚爲什麼。感覺有些事情顯而易見,我錯過了。
您正在記錄'responseData',但將值賦給'response'? – adrichman
這是一個錯字,我會糾正,在實際的代碼是正確的 – GRowing