【发布时间】:2015-11-16 00:50:08
【问题描述】:
我将 Angular 与 UI 路由器和 Firebase 一起使用。单个页面上有两种不同状态的两种表格:联系表格和信用卡表格。
当用户点击提交时,信用卡信息被提交给 Stripe。然后将联系表单提交给 Firebase,但前提是信用卡交易成功完成。以下代码在开发中有效。但是当代码被缩小时,联系表单永远不会提交。
对我做错了什么有什么想法吗?
联系表格的控制器:
.controller('ContactFormCtrl', ['$scope', 'Contacts', 'serviceB', function ($scope, Contacts, serviceB) {
var contactForm = this;
var stripeDone = serviceB.get();
contactForm.contact = {};
contactForm.contacts = Contacts;
$scope.$watch(serviceB.get, function(stripeDone) {
if (stripeDone === 'yes') {
console.log(contactForm.contact);
Contacts.$add(contactForm.contact)
} else {
console.log('Card not charged');
}
}])
信用卡表单控制器:
.controller('PaymentFormCtrl', ['$scope', '$http', 'serviceB', function ($scope, $http, serviceB) {
$scope.handleStripe = function (status, response) {
var stripeDone='yes';
return $http.post(http://localhost:9000/api/payments, JSON.stringify(response))
.then(function() {
serviceB.set(stripeDone);console.log('serviceB set now',stripeDone);})
.then(function() {$scope.payment={};
})
.then(function() {$state.go('thankyou');})
}]);
ServiceB 服务:
(function() {
'use strict';
angular.module('App')
// ServiceB confirms that credit card info was submitted to Stripe
.service('serviceB', serviceB);
function serviceB () {
var status = null;
return {
get: function () {
return status;
},
set: function (value) {
status = value;
}
};
}
})();
联系工厂:
(function() {
'use strict';
angular.module('contacts.fact', [])
.factory('Contacts', ['$firebaseArray', '$q', 'FBURL', 'Auth', 'Ref', function ($firebaseArray, $q, FBURL, Auth, Ref) {
var authData = Ref.getAuth();
var ref = new Firebase(FBURL + '/contacts/' + authData.uid);
return $firebaseArray(ref);
}]);
})();
路由器信息:
.state('payment.details', {
url: '/details',
views: {
'top': {
templateUrl: 'app/views/contact-form.html',
controller: 'ContactFormCtrl as contactForm'
},
'bottom': {
templateUrl: 'app/views/credit-card.html',
controller: 'PaymentFormCtrl'
// Note: not using controllerAs syntax because Angular Payments module does not support it
},
}
})
更新:如果我将.then(function() {$state.go('thankyou');}) 从信用卡表单的控制器中移出,并将其放在联系人表单控制器中Contacts.$add(contactForm.contact) 的末尾,那么一切正常。这解决了问题,但我怀疑这是正确的解决方案。有什么想法吗?
【问题讨论】:
-
听起来您在 ContactFormCtrl 中的 $watch 有问题。控制台日志在缩小时是否打印任何内容?如果不是,则 serviceB.get 位出现问题。
-
@KumarM 感谢您的意见。不,contactFormCtrl 中的
console.log()不打印任何内容。我想我在那里的 $watch 做错了什么,但不知道是什么。 -
$scope.$watch(serviceB.get,应该改为$scope.$watch(serviceB.get(),。 -
@Kato 我按照您上面的建议修改了代码,但仍然没有运气。
-
当你说它在缩小时不起作用,究竟是什么错误?
标签: angularjs firebase angularfire angular-ui-router