angularJS 中$attrs方法使用指南
這里給大家分享的是一個(gè)angularJS 中$attrs方法的使用示例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>
無(wú)標(biāo)題文檔
</title>
<script src="http://localhost:81/js/jquery.js">
</script>
<script src="http://localhost:81/js/angular.min.js">
</script>
</head>
<body ng-app="Demo">
<div a>
a_directive
</div>
<div ng-controller="TestCtrl">
<h1 t>
原始內(nèi)容
</h1>
<h2 t2>
原始內(nèi)容
</h2>
<h3 t3="hiphop" title2="{{name}}">
原始內(nèi)容
</h3>
<div compile></div>
<div>
<test a="{{ a }}" b c="xxx"></test>
<button ng-click="a=a+1">
修改
</button>
</div>
<te a="1" ys-a="123" ng-click="show(1)">這里</te>
</div>
<script>
var app = angular.module('Demo', [], angular.noop);
app.controller("TestCtrl",
function($scope) {
$scope.name = "qihao";
});
app.directive("t",
function() {
return {
controller : function($scope){$scope.name = "qq"},
template : "<div>test:implementToParent{{name}}</div>",
replace : true,
scope : true //作用域是繼承的,默認(rèn)就是繼承的
}
});
app.directive("t2",
function() {
return {
controller : function($scope){$scope.name = "nono"},
template : "<div>test:implementToParent{{name}}</div>",
replace : true,
restrict : "AE"
}
});
app.directive("t3",
function() {
return {
template : "<div>test:implementToParent_titleIs:{{title}}<br>title2Is:{{title2}}</div>",
replace : true,
restrict : "AE",
scope : {
title : "@t3",
title2 : "@title2"
}
}
});
app.directive('a',
function() {
var func = function() {
console.log('compile');
return function() {
console.log('link');
}
}
var controller = function($scope, $element, $attrs, $transclude) {
//$transclude :是指令標(biāo)簽的復(fù)制體
console.log('controller');
console.log($scope);
console.log($transclude);
//$transclude接受兩個(gè)參數(shù),你可以對(duì)這個(gè)克隆的元素進(jìn)行操作,
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);
}
return {
compile: func,
template: "<h1 ng-transclude></h1>",
controller: controller,
transclude: true,
restrict: 'AE'
}
});
app.directive('compile',function() {
var func = function() {
console.log('a compile');
return {
pre: function() {
console.log('a link pre')
},
post: function() {
console.log('a link post')
},
}
}
return {
restrict : "AE",
compile : 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,
restrict: 'E'}
});
app.controller('TestCtrl', function($scope){
$scope.a = 123;
});
app.directive('te', function(){
var func = function($scope,$element, $attrs,$ctrl){
console.log($ctrl)
//$attrs.$set. 給這個(gè)屬性設(shè)置b,值為ooo,就是這樣
$attrs.$set('b', 'ooo');
$attrs.$set('a-b', '11');
//這個(gè)還有點(diǎn)不懂啊 //第二個(gè)參數(shù)值
$attrs.$set('c-d', '11', true, 'c_d');
console.log($attrs);
}
return {
compile: function(){
return func
},
restrict: 'E'
}
});
app.controller('TestCtrl', function($scope){
$scope.show = function(v){console.log(v);}
});
</script>
</body>
</html>
本文內(nèi)容就到這里了,希望大家能對(duì)angularJS 中$attrs的使用有了新的認(rèn)識(shí),希望大家能夠喜歡本文。
相關(guān)文章
Angular設(shè)置title信息解決SEO方面存在問(wèn)題
爬蟲(chóng)在檢索seo信息的時(shí)候會(huì)讀不了js給其賦的值,導(dǎo)致搜索引擎收錄不了或者收錄了無(wú)效的信息,下面本文給大家介紹Angular設(shè)置title信息解決SEO方面存在問(wèn)題,需要的朋友可以參考下2016-08-08AngularJS ng-repeat數(shù)組有重復(fù)值的解決方法
不知道大家是否遇到過(guò)這個(gè)問(wèn)題,在當(dāng)Angular.JS ng-repeat數(shù)組中有重復(fù)項(xiàng)時(shí),系統(tǒng)就會(huì)拋出異常,這是該怎么做?本文通過(guò)示例代碼介紹了詳細(xì)的解決方法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-10-10AngularJs實(shí)現(xiàn)分頁(yè)功能不帶省略號(hào)的代碼
這篇文章主要介紹了AngularJs實(shí)現(xiàn)分頁(yè)功能不帶省略號(hào)的代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-05-05Angularjs實(shí)現(xiàn)控制器之間通信方式實(shí)例總結(jié)
這篇文章主要介紹了Angularjs實(shí)現(xiàn)控制器之間通信方式,結(jié)合實(shí)例形式總結(jié)分析了AngularJS控制器常用通信方式及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-03-03angular ng-model 無(wú)法獲取值的處理方法
今天小編就為大家分享一篇angular ng-model 無(wú)法獲取值的處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10解決angularjs前后端分離調(diào)用接口傳遞中文時(shí)中文亂碼的問(wèn)題
今天小編就為大家分享一篇解決angularjs前后端分離調(diào)用接口傳遞中文時(shí)中文亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08AngularJS頁(yè)面訪(fǎng)問(wèn)時(shí)出現(xiàn)頁(yè)面閃爍問(wèn)題的解決
這篇文章主要介紹了AngularJS框架使用中出現(xiàn)頁(yè)面閃爍問(wèn)題的解決方法,閃爍問(wèn)題一般是初始化未加載完畢造成的,需要的朋友可以參考下2016-03-03