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