ここでは、Angularjsで$ attrsメソッドを使用する例を共有しています。
コードコピーは次のとおりです。
<!doctype html>
<html>
<head>
<メタcharset = "utf-8">
<title>
無題のドキュメント
</title>
<スクリプトsrc = "http:// localhost:81/js/jquery.js">
</script>
<スクリプトsrc = "http:// localhost:81/js/angular.min.js">
</script>
</head>
<body ng-app = "demo">
<diva>
A_Directive
</div>
<div ng-controller = "testctrl">
<h1t>
オリジナルコンテンツ
</h1>
<H2 T2>
オリジナルコンテンツ
</h2>
<h3 t3 = "hiphop" title2 = "{{name}}">
オリジナルコンテンツ
</h3>
<div compile> </div>
<div>
<test a = "{{a}}" bc = "xxx"> </test>
<button ng-click = "a = a+1">
改訂
</button>
</div>
<te a = "1" ys-a = "123" ng-click = "show(1)">ここ</te>
</div>
<スクリプト>
var app = angular.module( 'demo'、[]、angular.noop);
app.controller( "testctrl"、
function($ scope){
$ scope.name = "qihao";
});
app.directive( "t"、
関数() {
戻る {
コントローラー:function($ scope){$ scope.name = "qq"}、
テンプレート: "<div> test:実装{{name}} </div>"、
交換:本当、
スコープ:true //スコープが継承され、デフォルトは継承されます
}
});
app.directive( "t2"、
関数() {
戻る {
コントローラー:function($ scope){$ scope.name = "nono"}、
テンプレート: "<div> test:実装{{name}} </div>"、
交換:本当、
制限:「AE」
}
});
app.directive( "t3"、
関数() {
戻る {
テンプレート: "<div> test:実装toparent_titleis:{{title}} <br> title2is:{{title2}} </div>"、
交換:本当、
制限:「AE」、
スコープ:{
タイトル:「@T3」、
タイトル2: "@title2"
}
}
});
app.directive( 'a'、
関数() {
var func = function(){
console.log( 'compile');
return function(){
console.log( 'link');
}
}
var controller = function($ scope、$ element、$ attrs、$ transclude){
// $ transclude:命令タグのコピーです
console.log( 'Controller');
console.log($ scope);
console.log($ transclude);
// $ transcludeは2つのパラメーターを受け入れ、クローン化された要素で動作できます。
var node = $ transclude(function(clone_element、scope){
$ element.append(clone_element);
$ element.Append( "<span> spantag ___ </span>");
console.log(clone_element);
console.log( ' - ');
console.log(scope);
});
console.log(node);
}
戻る {
コンパイル:FUNC、
テンプレート: "<h1 ng-transclude> </h1>"、
コントローラー:コントローラー、
transclude:本当、
制限:「AE」
}
});
app.directive( 'compile'、function(){
var func = function(){
console.log( 'a compile');
戻る {
pre:function(){
console.log( 'リンクpre')
}、
投稿:function(){
console.log( 'リンク投稿')
}、
}
}
戻る {
制限:「AE」、
コンパイル:FUNC
}
})
app.directive( 'test'、function(){
var func = function($ element、$ attrs){
console.log($ attrs);
$ attrs。$ observe( 'a'、function(new_v){
console.log(new_v);
});
}
return {compile:func、
制限: 'e'}
});
App.Controller( 'testctrl'、function($ scope){
$ scope.a = 123;
});
app.directive( 'te'、function(){
var func = function($ scope、$ element、$ attrs、$ ctrl){
console.log($ ctrl)
//$ttrs.$set。このプロパティのBを設定してください、値はooooです、それだけです
$ attrs。$ set( 'b'、 'ooo');
$ attrs。$ set( 'a-b'、 '11');
//私はまだこれを理解していません// 2番目のパラメーター値
$ attrs。$ set( 'c-d'、 '11'、true、 'c_d');
console.log($ attrs);
}
戻る {
コンパイル:function(){
funcを返します
}、
制限:「E」
}
});
App.Controller( 'testctrl'、function($ scope){
$ scope.show = function(v){console.log(v);}
});
</script>
</body>
</html>
この記事はすべてです。 Angularjsでの$ attrsの使用を新しい理解できることを願っています。この記事を気に入っていただければ幸いです。