我在控制器内部有一些动作。它包含一个Promise对象,在解析时,应该通过controllerAs绑定更新视图。问题是已解析的值是正确的(在控制台中记录),但将其分配给控制器并不会更新视图。它仅在包含该承诺的操作再次执行时更新。因此,如果我按下按钮继续执行它,则更新会延迟一次执行。
我正在使用AngularJS 1.6.5。我没有使用$ q。也没有$ scope,所以请不要申请$。
我的简化控制器有2个成员来比较有和没有承诺的行为:
var vm = this;
vm.textSync = 0;
vm.textAsync = 0;
vm.onclick = function()
{
var action = new Promise(function(resolve, reject)
{
resolve(vm.textAsync + 1);
});
action.then(function(result)
{
console.log(result);
vm.textAsync = result;
});
vm.textSync++;
};
记录的值等于递增的textSync(1,2,3,...),但是在eack点击后,视图中的textAsync等于(textSync-1)(所以它是0,1,1 ,2,...)。为什么这样以及如何解决它?
我的简化观点:
<input type="button" value="Click me" ng-click="$ctrl.onclick()" />
<input ng-model="$ctrl.textSync" />
<input ng-model="$ctrl.textAsync" />
答案 0 :(得分:0)
由于你不想触发摘要周期,你可以使用$ timeout和0秒来等待摘要周期,所以请像这样包装你的承诺,只需在函数后省略0 - 不必要:
# set variables
num_epochs = 15
tweet_size = 20
hidden_size = 200
vec_size = 300
batch_size = 512
number_of_layers= 1
number_of_classes= 3
learning_rate = 0.001
TRAIN_DIR="/checkpoints"
tf.reset_default_graph()
# Create a session
session = tf.Session()
# Inputs placeholders
tweets = tf.placeholder(tf.float32, [None, tweet_size, vec_size], "tweets")
labels = tf.placeholder(tf.float32, [None, number_of_classes], "labels")
# Placeholder for dropout
keep_prob = tf.placeholder(tf.float32)
# make the lstm cells, and wrap them in MultiRNNCell for multiple layers
def lstm_cell():
cell = tf.contrib.rnn.BasicLSTMCell(hidden_size)
return tf.contrib.rnn.DropoutWrapper(cell=cell, output_keep_prob=keep_prob)
multi_lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(number_of_layers)], state_is_tuple=True)
# Creates a recurrent neural network
outputs, final_state = tf.nn.dynamic_rnn(multi_lstm_cells, tweets, dtype=tf.float32)
with tf.name_scope("final_layer"):
# weight and bias to shape the final layer
W = tf.get_variable("weight_matrix", [hidden_size, number_of_classes], tf.float32, tf.random_normal_initializer(stddev=1.0 / math.sqrt(hidden_size)))
b = tf.get_variable("bias", [number_of_classes], initializer=tf.constant_initializer(1.0))
sentiments = tf.matmul(final_state[-1][-1], W) + b
prob = tf.nn.softmax(sentiments)
tf.summary.histogram('softmax', prob)
with tf.name_scope("loss"):
# define cross entropy loss function
losses = tf.nn.softmax_cross_entropy_with_logits(logits=sentiments, labels=labels)
loss = tf.reduce_mean(losses)
tf.summary.scalar("loss", loss)
with tf.name_scope("accuracy"):
# round our actual probabilities to compute error
accuracy = tf.to_float(tf.equal(tf.argmax(prob,1), tf.argmax(labels,1)))
accuracy = tf.reduce_mean(tf.cast(accuracy, dtype=tf.float32))
tf.summary.scalar("accuracy", accuracy)
# define our optimizer to minimize the loss
with tf.name_scope("train"):
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
#tensorboard summaries
merged_summary = tf.summary.merge_all()
logdir = "tensorboard/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + "/"
writer = tf.summary.FileWriter(logdir, session.graph)
# initialize any variables
tf.global_variables_initializer().run(session=session)
# Create a saver for writing training checkpoints.
saver = tf.train.Saver()
# load our data and separate it into tweets and labels
train_tweets = np.load('data_es/train_vec_tweets.npy')
train_labels = np.load('data_es/train_vec_labels.npy')
test_tweets = np.load('data_es/test_vec_tweets.npy')
test_labels = np.load('data_es/test_vec_labels.npy')
**HERE I HAVE THE LOOP FOR TRAINING AND TESTING, I KNOW ITS FINE**