带有 ngModel 绑定的 AngularJS 自定义指令不起作用
AngularJS custom directive with ngModel binding not working
我正在尝试创建一个自定义指令来显示格式化的地址。我已经编写了以下代码来使用 new 指令,但它不起作用。我的帐户控制器(未显示)工作正常,并且计费地址.Line1 显示正确。但是,我的地址指令不会呈现任何内容。
我已经在我的 html 中包含指令代码,尽管我希望将其移动到单独的 .js 文件中以供重用。有人可以解释我做错了什么吗?
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script type="text/javascript" src="ng-address.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
(function() {
var app = angular.module('ng-directives', []);
app.directive("ngAddress", function() {
return {
restrict: 'E',
scope: {
address: '=ngModel'
},
template: "<div>{{address.Line1}}</br><span>{{address.Line1}}</br></span>{{address.city}}, {{address.state}} {{address.postCode}}</div>"
};
});
})();
</script>
</head>
<body class="container" ng-controller="AccountController as account">
<div>{{account.model.billingAddress.line1}}</div>
<ng-address ng-model="account.model.billingAddress"></ng-address>
</body>
</html>
首先,该示例不完整。
- 我将角度模块更改为
myApp
- 我添加了一个控制器
AccountController
- 在ng-controller中使用
as
不起作用。我不确定这是否应该有效,但我从不使用它。
该指令的主要问题是ng-model
使用不当。您应该将其传递给require
,而不是范围。然后将其传递给link
函数,在该函数中实现$render
函数。
您可能还想将函数推送到 $viewChangeListeners
。
有关更多详细信息,请参阅文档。
最后模板包含</br>
,这是无效的。
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script type="text/javascript">
(function() {
var app = angular.module('myApp', []);
app.controller('AccountController', function($scope) {
$scope.model = {
billingAddress: {
line1: 'somewhere far far away',
city: 'Dark side of the moon',
state: 'Top secret',
postCode: '1337XD'
}
};
});
app.directive("ngAddress", function() {
return {
restrict: 'E',
require: '?ngModel',
template: "<div>{{address.line1}}<br><span>{{address.line1}}<br></span>{{address.city}}, {{address.state}} {{address.postCode}}</div>",
link: function(scope, element, attrs, ngModel) {
ngModel.$render = function() {
scope.address = ngModel.$modelValue;
};
}
};
});
})();
</script>
</head>
<body class="container" ng-controller="AccountController">
<input class="form-control" ng-model="model.billingAddress.line1"/>
<input class="form-control" ng-model="model.billingAddress.city"/>
<input class="form-control" ng-model="model.billingAddress.state"/>
<input class="form-control" ng-model="model.billingAddress.postCode"/>
<ng-address ng-model="model.billingAddress"></ng-address>
</body>
</html>
相关文章:
- 如何在自定义指令中获取计算的属性
- 向动态生成的DOM添加Angular自定义指令
- 自定义指令中的AngularJS ng模型
- 如何在html中以角度显示自定义指令的作用域
- 自定义指令模板中的AngularJS控制变量
- AngularJS通过自定义指令传递点击的项目
- 如何在angularjs中将多个作用域变量传递到自定义指令中
- 处理角度自定义指令中的onReady事件
- 为什么ng中的自定义指令在循环完成后重复运行
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- Angular.JS自定义指令;不起作用
- 如何通过Angular中的$resource发送来自自定义指令的输入值
- AngularJS:如何访问自定义指令中的input[date]min属性
- AngularJS:ngRepeat作用域在具有隔离作用域和ngTransclude的自定义指令中失败
- 如何使用其他自定义指令中的元素标记作为另一个自定义指令的模板
- 如何限制自定义指令的可能属性值
- 动态网格作为自定义指令
- AngularJS - 在自定义指令中继承ngModel
- 调用另一个自定义指令的角度自定义指令
- 在 AngularJs 中验证自定义指令(我想让它成为必需的)