如何在 Angular 1.x 中的输入字段上使用自定义指令?
How to use custom directive on Input fields in Angular 1.x?
我正在尝试使用自定义 AngularJS 指令修改 input
元素。基本上我想用国家下拉列表替换任何 <input type="country">
字段。
但该指令似乎不适用于 input
字段。如果我将其更改为任何其他标签,它会起作用吗?
代码如下:
angular.module('plunker', [])
.controller('MainCtrl', function ($scope) {
$scope.name = 'World';
});
angular.module('plunker')
.directive('input', function() {
return {
restrict: 'E',
scope: {ngModel: '=?'},
link: function(scope, elem, attr) {
if(attr.type === 'country') {
elem.html('html code for select');
alert(elem);
}
}
};
});
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
Name: <input type="country" ng-model="name"/> <br/>
</body>
</html>
有人可以解释并提出解决方法吗?
P.S。我也试过在指令中这样做,但它也不起作用!
replace: true,
template:'<div>hello</div>'
P.S。我知道我可以使用 ng-country
或其他一些自定义标签,但我想更改 input
标签只是因为我想了解为什么会发生这种情况或可能找出我做错了什么?
最新更新:
您的代码只是在元素上设置 html,而不是替换它。你会想像这样使用 replaceWith:
var app = angular.module("TestApp",[]);
app.controller("TestController", function($scope){
$scope.message = "Input Directive Test"
});
app.directive("input", function() {
return {
restrict: "E",
link: function(scope, elem, attr) {
if(attr.type === "country") {
var select = angular.element("<select><option>USA</option></select>");
elem.replaceWith(select);
}
}
}
});
这是 JSBin:https://jsbin.com/juxici/4/edit?html,js,output
我的回答的初始版本:
这是一个在使用 'replace' 和 'template' 时没有任何问题的示例。我没有检查类型等,但您可以在链接器代码中执行此操作。
JSBin: https://jsbin.com/juxici/2/edit?html,js,output
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
</head>
<body ng-app="TestApp">
<div ng-controller="TestController">
<h2>{{message}}</h2>
<input type="country" ng-model="name"/>
</div>
</body>
</html>
Javascript:
var app = angular.module("TestApp",[]);
app.controller("TestController", function($scope){
$scope.message = "Input Directive Test"
});
app.directive("input", function() {
return {
restrict: "E",
replace: true,
template:"<select><option>USA</option></select>"
}
});
我正在尝试使用自定义 AngularJS 指令修改 input
元素。基本上我想用国家下拉列表替换任何 <input type="country">
字段。
但该指令似乎不适用于 input
字段。如果我将其更改为任何其他标签,它会起作用吗?
代码如下:
angular.module('plunker', [])
.controller('MainCtrl', function ($scope) {
$scope.name = 'World';
});
angular.module('plunker')
.directive('input', function() {
return {
restrict: 'E',
scope: {ngModel: '=?'},
link: function(scope, elem, attr) {
if(attr.type === 'country') {
elem.html('html code for select');
alert(elem);
}
}
};
});
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
Name: <input type="country" ng-model="name"/> <br/>
</body>
</html>
有人可以解释并提出解决方法吗?
P.S。我也试过在指令中这样做,但它也不起作用!
replace: true,
template:'<div>hello</div>'
P.S。我知道我可以使用 ng-country
或其他一些自定义标签,但我想更改 input
标签只是因为我想了解为什么会发生这种情况或可能找出我做错了什么?
最新更新:
您的代码只是在元素上设置 html,而不是替换它。你会想像这样使用 replaceWith:
var app = angular.module("TestApp",[]);
app.controller("TestController", function($scope){
$scope.message = "Input Directive Test"
});
app.directive("input", function() {
return {
restrict: "E",
link: function(scope, elem, attr) {
if(attr.type === "country") {
var select = angular.element("<select><option>USA</option></select>");
elem.replaceWith(select);
}
}
}
});
这是 JSBin:https://jsbin.com/juxici/4/edit?html,js,output
我的回答的初始版本:
这是一个在使用 'replace' 和 'template' 时没有任何问题的示例。我没有检查类型等,但您可以在链接器代码中执行此操作。
JSBin: https://jsbin.com/juxici/2/edit?html,js,output
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
</head>
<body ng-app="TestApp">
<div ng-controller="TestController">
<h2>{{message}}</h2>
<input type="country" ng-model="name"/>
</div>
</body>
</html>
Javascript:
var app = angular.module("TestApp",[]);
app.controller("TestController", function($scope){
$scope.message = "Input Directive Test"
});
app.directive("input", function() {
return {
restrict: "E",
replace: true,
template:"<select><option>USA</option></select>"
}
});