2

私はinput要素を、カスタムのAngularJS命令を使って修正しようとしています。基本的には、<input type="country">フィールドを国のドロップダウンに置き換えたいとします。Angular 1.xの入力フィールドでカスタムディレクティブを使用するにはどうすればよいですか?

しかし、指示は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タグを変更する必要があるのは、なぜこれが起こっているのか、おそらく私が間違っていることを知りたいからです。

+0

は、代わりに属性としてそれを宣言しない理由を要素としてあなたのディレクティブを宣言する? – stevenelberger

+0

クラス/属性レベルとカスタムディレクティブ名には、myInputなどの制限を与える必要があります。 –

答えて

2

最新のアップデート:

あなたのコードは、単に要素にHTMLを設定するのではなく、それを置き換えています。代わりに、このようreplaceWithを使用するとよいでしょう:私の答えのhttps://jsbin.com/juxici/4/edit?html,js,output

初期バージョン:交換する際に「ここ

が問題なく動作する例です

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です'と' 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>" 
    } 
}); 
+0

ありがとう!それは動作しているようですが、まだ1つの問題があります。私が '$ compile'を要素に実行すると、まだ動作していないようですか?何か案は? – supersan

+0

@supersanなぜあなたは$ compileを使用していますか?いくつかのコードを表示します。この回答はあなたのオリジナルの質問に役立ちましたか?質問を延長していますか?ただ理解しようとしている。 – KumarM

+0

こんにちは、このコードを参照してください:https://jsbin.com/yiyideyomu/edit?html,js,output(私は選択に 'ng-options'を使用したいのでコンパイルする必要があります) – supersan

関連する問題