我有一个带有两个功能的控制器,它们两个都在工厂中称我为一个功能:
.controller("Ctrl",function($scope,fetchDataFromServer){
$scope.dataArray = [];
$scope.dataArray2 = [];
$scope.addNew = function () {
fetchDataFromServer.fetchData()
.then(function (data) {
console.log(data);
// $scope.dataArray.push(data);
});
};
$scope.addNew2 = function() {
console.log(fetchDataFromServer.fetchData());
}
})
我有一个工厂,可以从api中获取数据:
.factory('fetchDataFromServer', function ($http, $q) {
return {
fetchData: function() {
return $http.get('//jsonplaceholder.typicode.com/posts/1')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
})
console.log
添加addNew给我一个对象:
对象{userId:1,id:1,title:“ sunt”,body:“ quia”}
但是我在控制器中给出了这个结果:
{…} “ $$ state”:{…} 状态:1 值:对象{userId:1,id:1,标题:“不能自动取消公积金,应有尽职”,…} :对象{…} :对象{然后:then() ,catch:catch(),finally:finally(),...}
任何人都可以为我解释为什么结果不同以及造成差异的原因是什么?
我在plunker上的代码
答案 0 :(得分:3)
在第二功能中,您正在打印承诺,而在第一功能中,您正在打印已解决的价值/承诺的结果。
public class SendMail extends AsyncTask<Void,Void,Void> {
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
private static int SPLASH_TIME_OUT = 3000;
//Send button
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com", "password");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("abc@gmail.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}}