Javascript 如果显示保存和更新按钮,则AngularJS处于条件ng 名称 电子邮件 电话
考虑上面的表单,在此创建/编辑中使用的是相同的模板,因此我想显示 基于newcontact.id值,按钮名称为“更新”或“保存” 只需关注以下几点:Javascript 如果显示保存和更新按钮,则AngularJS处于条件ng 名称 电子邮件 电话,javascript,angularjs,Javascript,Angularjs,考虑上面的表单,在此创建/编辑中使用的是相同的模板,因此我想显示 基于newcontact.id值,按钮名称为“更新”或“保存” 只需关注以下几点: <form novalidate> <label>Name</label> <input type="text" name="name" ng-model="newcontact.name" required/> <label>Email</labe
<form novalidate>
<label>Name</label>
<input type="text" name="name" ng-model="newcontact.name" required/>
<label>Email</label>
<input type="text" name="email" ng-model="newcontact.email" required/>
<label>Phone</label>
<input type="text" name="phone" ng-model="newcontact.phone" required/>
<br/>
<input type="hidden" ng-model="newcontact.id" />
<input type="button" ng-if="newcontact.id" value="UPDATE" ng-click="saveContact()" />
<input type="button" ng-if="!newcontact.id" value="SAVE" ng-click="updateContact()" />
</form>
既然没有其他的ng,那么如何做到这一点
也需要知道。
2.如何在AngularJS中保护用户的业务逻辑
您只需使用按钮,即可在此处使用ng show
:
<input type="button" ng-if="newcontact.id" value="UPDATE" ng-click="saveContact()" />
<input type="button" ng-if="!newcontact.id" value="SAVE" ng-click="updateContact()" />
编辑:另一个选项可以是不一次性决定是否为新联系人:
angular.module('my.module').controller(['$scope', function($scope) {
$scope.save = function() {
if(typeof scope.newcontact.id === 'undefined') {
// save
} else {
// update
}
}
}]);
注意:这只是一个粗略的例子,您可能应该将服务与API结合使用来保存您的用户。该示例所做的基本上是观察范围属性并相应地更改标签。有关更详细的说明,请参阅
然后以以下形式使用:
angular.module('my.module').controller(['$scope', function($scope) {
$scope.newcontact = {};
$scope.save = function() {
/** Your save function */
}
// with a set contact, this will now always evaluate to 'Save'
$scope.label = !!scope.newcontact.id ? 'Update' : 'Save';
// so we go for something different:
$scope.$watch('newcontact', function(newValue) {
//this watches the newcontact for a change, 'newValue' is the new state of the scope property
$scope.label = !!newValue.id ? 'Update' : 'Save';
});
}]);
{{label}
编辑2:回答您关于向用户隐藏业务逻辑的问题-您根本不能。AngularJS是一个JavaScript框架,您可以为用户浏览器提供运行应用程序的完整代码。如果您确实有敏感逻辑,请将其放在服务器端,并向其公开一个接口(例如,通过JSON API)。好
您只需使用按钮,即可在此处使用ng show
:
<input type="button" ng-if="newcontact.id" value="UPDATE" ng-click="saveContact()" />
<input type="button" ng-if="!newcontact.id" value="SAVE" ng-click="updateContact()" />
编辑:另一个选项可以是不一次性决定是否为新联系人:
angular.module('my.module').controller(['$scope', function($scope) {
$scope.save = function() {
if(typeof scope.newcontact.id === 'undefined') {
// save
} else {
// update
}
}
}]);
注意:这只是一个粗略的例子,您可能应该将服务与API结合使用来保存您的用户。该示例所做的基本上是观察范围属性并相应地更改标签。有关更详细的说明,请参阅
然后以以下形式使用:
angular.module('my.module').controller(['$scope', function($scope) {
$scope.newcontact = {};
$scope.save = function() {
/** Your save function */
}
// with a set contact, this will now always evaluate to 'Save'
$scope.label = !!scope.newcontact.id ? 'Update' : 'Save';
// so we go for something different:
$scope.$watch('newcontact', function(newValue) {
//this watches the newcontact for a change, 'newValue' is the new state of the scope property
$scope.label = !!newValue.id ? 'Update' : 'Save';
});
}]);
{{label}
编辑2:回答您关于向用户隐藏业务逻辑的问题-您根本不能。AngularJS是一个JavaScript框架,您可以为用户浏览器提供运行应用程序的完整代码。如果您真的有敏感的逻辑,请将其放在服务器端,并向其公开一个接口(例如,通过JSON API)。除非我缺少明显的,否则您拥有的对我来说很好。除非我缺少明显的,否则您拥有的对我来说很好。+1作为第二个解决方案,因为这让我知道了如何做,您的$scope.label=!!scope.newcontact.id?“更新':'保存';如果newcontact未定义,则将无法工作,
/***/代码>用于。基本上,您可以初始化newcontact
对象(如果该对象不存在),或者预先填充它(如果存在):$scope.newcontact={}代码>给我一分钟,我把它放在代码示例中。+1作为第二个解决方案,因为这给了我一个方法,您的$scope.label=!!scope.newcontact.id?“更新':'保存';如果newcontact未定义,则将无法工作,/***/代码>用于。基本上,您可以初始化newcontact
对象(如果该对象不存在),或者预先填充它(如果存在):$scope.newcontact={}代码>给我一分钟,我把它放在代码示例中。