如何根据条件为 ng-model 赋值
How to assign ng-model a value based on a condition
好的,我正在尝试创建一个Web链接指令。链接需要 URL 和文本值。如果文本值没有值 - 文本值将等于 url。例如:
<a href="http://google.com">http://google.com</a>
链接具有文本值的示例
<a href="http://google.com">Search engine</a>
下面的代码中有两个输入标记,用于记录 URL 值和文本值。如果文本值为空,则文本值将等于 url 值。这是我的指令的代码:
Website1.directive('inputlink', function () {
return {
restrict: 'E',
replace: false,
scope: {
urlvalue: '=',
textvalue: '='
},
template: '<div>' +
'<input type="text" ng-model="urlvalue" value="{{urlvalue}}" placeholder="Enter link URL"></input>'+
'<input type="text" ng-model="textvalue" value="{{textvalue}}" placeholder="Enter text"></input>' +
'</div>'
}
});
因此,如果文本值为空,它将等于urlvalue,否则文本值将具有自己的值。
您需要在函数中更改它。
link: function( $scope, $element, $attributes ) {
if(!$scope.textvalue) {
$scope.textvalue = $scope.urlvalue;
}
}
根据您的要求,您可能还需要$watch
它。
相关文章:
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- ng-model 绑定到选择标签生成的空默认值
- 如何在 AngularJS 中将 ng-model 值检索到控制器
- 使用 ID 更新文本区域值,而不是在 AngularJs 中使用 ng-model
- AngularJS - 指令填充文本框,但ng-model没有值
- 将 md-dialog 中的值绑定到指令中的 ng-model
- AngularJS ng-model 未设置正确的值
- 如果值为空,ng-model 将从对象中删除元素
- 如何根据条件为 ng-model 赋值
- AngularJS更新ng-model的值以设置默认值
- 如何将一个ng-model的值与一个ng-repeat的值连接起来
- AngularJS -无法从ng-model获取值到控制器
- Angular的ng-click无法获取ng-model的值
- 如果ng-model的值是从另一个ng-model分配的,则不能得到ng-model的值
- 从不同的对象分配ng-model输入值
- 根据字符串和do函数检查ng-model的值
- 通过javascript为ng-model设置值
- Angular ng-model的值没有在提交时注册
- 为什么按ng键不能改变ng-model的值?
- Angularjs点击已经选中的选项,改变ng-model的值