我对AngularJS很新。我必须对我们的网站进行一些非常小的改动,但它让我很难过。
我设法让所有事情都进入最后一步。我玩过许多潜在的解决方案,没有什么能够奏效。对于保存消息文本的元素,
$scope.apply()
更新元素,但因为它使用ng-bind
布尔值并不是很好:我不是想要只显示用户" true"或" false"。ng-model="user.myValue" ng-value="true"
并不有用,因为它没有有条件地显示消息...... 到目前为止,这是我的代码。
模板:
<div ng-controller="myCtrl">
<div id="modal-my-value-toggle" class="modal">
<a ng-click="myValueToggle('false')">No</a>
<a ng-click="myValueToggle('true')">Yes</a>
</div>
{% if user.myValue %}
<div name="myValueTrue">
Message A: Your value is Yes. <a ng-click="openModal()">click here to change your value</a>
</div>
{% else %}
<div name="myValueFalse">
Message B: Your value is No. <a ng-click="openModal()">click here to change your value</a>
</div>
{% endif %}
</div>
控制器:
function myCtrl ($scope, $http, Notification) {
$scope.username = context.targetUsername;
$scope.openModal = function() {
var $myModal = $('div').find('#modal-my-value-toggle');
$myModal.modal({
keyboard: false,
backdrop: "static"
});
$myModal.modal('show');
};
$scope.myValueToggle = function(userProvidedValue) {
// hide the modal
var $myModal = $('div').find('#modal-my-value-toggle');
$myModal.modal('hide');
if (userProvidedValue === 'true') {
var success = "Your value is now Yes";
} else {
var success = "Your value is now No";
}
// ping the api to set flag to true
$http({
method: 'GET',
url: '/api/user/' + $scope.username + '/myValue/?myValue=' + userProvidedValue,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
Notification.success(success);
}, function(response) {
Notification.error("There was an error.");
});
};
}
我试过的其他一些东西......
这是基于此https://coderwall.com/p/ybbopq/use-ng-value-for-ng-model-with-boolean-values它只显示两条消息,并不是我想要的。它意味着提供一个无线电输入,您可以在其间切换以更新$ scope中的值。
<div name="myValue_true" ng-model="user.myValue" ng-value="true">
Message A: Your value is Yes. <a ng-click="openModal()">click here to change your value</a>
</div>
<div name="myValue_false" ng-model="user.myValue" ng-value="false">
Message B: Your value is No. <a ng-click="openModal()">click here to change your value</a>
</div>
这是决定在初始页面加载时显示哪条消息的另一种方法。它会闪烁两条消息,然后删除一条消息。
<div ng-if="user.myValue">
Message A: Your value is Yes. <a ng-click="openModal()">click here to change your value</a>
</div>
<div ng-if="!user.myValue">
Message B: Your value is No. <a ng-click="openModal()">click here to change your value</a>
</div>
我也在控制器中尝试过这种笨拙的事情(没有工作):
function myCtrl ($scope, $http, Notification) {
$scope.username = context.targetUsername;
$scope.openModal = function() {
var $myModal = $('div').find('#modal-my-value-toggle');
$myModal.modal({
keyboard: false,
backdrop: "static"
});
$myModal.modal('show');
};
$scope.myValueToggle = function(userProvidedValue) {
// hide the modal
var $myModal = $('div').find('#modal-my-value-toggle');
$myModal.modal('hide');
var message_ids = {
'yes': 'myValue_true',
'no': 'myValue_false'
}
if (userProvidedValue === 'true') {
var success = "Your value is now Yes";
var show_message = message_ids['yes']
var hide_message = message_ids['no']
} else {
var success = "Your value is now No";
var show_message = message_ids['no']
var hide_message = message_ids['yes']
}
// ping the api to set flag to true
$http({
method: 'GET',
url: '/api/user/' + $scope.username + '/myValue/?myValue=' + userProvidedValue,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
$('div').find('#' + show_message).show();
$('div').find('#' + hide_message).hide();
Notification.success(success);
}, function(response) {
Notification.error("There was an error.");
});
};
}
我是否完全以错误的方式解决这个问题?我应该从消息元素中做出指令吗?或者是其他东西?任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
消息闪烁然后在ng-ifs中删除的原因是user.myValue在首次呈现页面时未定义。这可以通过执行以下操作来解决:
将以下功能添加到控制器
$scope.checkMyValue = function() {
if (typeof $scope.user == 'undefined') {
return false; // Prevent error on check of undefined user
} else {
return typeof $scope.user.myValue != 'undefined';
}
};
然后您可以更改ng-if
ng-if="!user.myValue && checkMyValue()"