.directive('unorderedList', function () {
return {
link: function (scope, element, attrs) {
var data = scope[attrs['unorderedList'] || attrs['listSource'] ];
var propertyName = attrs['listProperty'] || "price || currency";
if(angular.isArray(data)){
var listElem = angular.element("<ul>");
if(element[0].nodeName == "#comment"){
element.parent().append(listElem);
}else{
element.append(listElem);
}
for(var i=0, len=data.length; i<len; i++){
var itemElem = angular.element('<li>').text(scope.$eval(propertyName, data[i]));
listElem.append(itemElem);
}
}
},
restrict:'EACM'
};
});
<unordered-list list-source="products" list-property="price | currency" />
var data = scope[attrs['unorderedList'] || attrs['listSource'] ];
<div unordered-list="products" list-property="price | currency"></div>
<div class="unordered-list: products" list-property="price | currency"></div>
<!-- directive: unordered-list products -->
.directive('unorderedList', function () {
return {
link: function (scope, element, attrs) {
scope.data = scope[attrs['unorderedList']];
},
restrict: 'A',
template:"<ul><li ng-repeat='item in data'>{{item.price | currency}}</li></ul>"
};
});
<script type="text/javascript" id="listTemplate">
<ul>
<li ng-repeat="item in data">{{item.price | currency}}</li>
</ul>
</script>
<script>
var myApp = angular.module('myApp', [])
.controller('myCtrl', ["$scope", function ($scope) {
$scope.products = [
{ name: "Apples", category: "Fruit", price: 1.20, expiry: 10 },
{ name: "Bananas", category: "Fruit", price: 2.42, expiry: 7 },
{ name: "Pears", category: "Fruit", price: 2.02, expiry: 6 }
];
}])
.directive('unorderedList', function () {
return {
link: function (scope, element, attrs) {
scope.data = scope[attrs['unorderedList']];
},
restrict: 'A',
template:function () {
return angular.element(
document.querySelector("#listTemplate")
).html();
}
};
});
</script>
<p>This is the form the template file</p>
<ul>
<li ng-repeat="item in data">{{item.price | currency}}</li>
</ul>
.directive('unorderedList', function () {
return {
link: function (scope, element, attrs) {
scope.data = scope[attrs['unorderedList']];
},
restrict: 'A',
templateUrl:"itemTemplate.html"
};
});
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in data">
<td>{{item.name}}</td>
<td>{{item.price | currency}}</td>
</tr>
</tbody>
</table>
<div unordered-list="products" template="table" class="table table-striped"> This is where the list will go </div>
.directive('unorderedList', function () {
return {
link: function (scope, element, attrs) {
scope.data = scope[attrs['unorderedList']];
},
restrict: 'A',
templateUrl: function (elem, attrs) {
return attrs['template'] == "table" ? "tableTemplate.html" : "itemTemplate.html";
}
};
});
<div class="panel panel-default"> <div class="panel-body" scope-demo></div> <div class="panel-body" scope-demo></div> </div>
var myApp = angular.module('myApp', [])
.controller('myCtrl', ["$scope", function ($scope) {
$scope.data = {name:"Staven"};
$scope.city = "China"
}])
.directive('scopeDemo', function () {
return {
template: function () {
return angular.element(document.querySelector("#scopeTemplate")).html();
},
scope:true
};
});
.directive('scopeDemo', function () {
return {
template: function () {
return angular.element(document.querySelector("#scopeTemplate")).html();
},
scope:{}
};
});
<body ng-app="myApp" ng-controller="myCtrl">
<div class="panel panel-default">
<div class="panel-body">
Direct Binding:<input ng-model="data.name" />
</div>
<div class="panel-body" scope-demo nameprop="{{data.name}}"></div>
</div>
</body>
<script type="text/ng-template" id="scopeTemplate">
<div class="panel-body">
<p>Data Value:{{local}}</p>
</div>
</script>
<script>
var myApp = angular.module('myApp', [])
.controller('myCtrl', ["$scope", function ($scope) {
$scope.data = {name:"staven"};
}])
.directive('scopeDemo', function () {
return {
template: function () {
return angular.element(document.querySelector("#scopeTemplate")).html();
},
scope:{
local:"@nameprop"
}
};
});
</script>
<div class="panel panel-default">
<div class="panel-body">
Direct Binding:<input ng-model="data.name" />
</div>
<div class="panel-body" scope-demo nameprop="data.name"></div>
</div>
<script type="text/ng-template" id="scopeTemplate">
<div class="panel-body">
<p>Data Value:<input ng-model="local" /></p>
</div>
</script>
<script>
scope:{
local:"=nameprop"
}
</script>
<body ng-app="myApp" ng-controller="myCtrl">
<div class="panel panel-default">
<div class="panel-body">
Direct Binding:<input ng-model="data.name" />
</div>
<div class="panel-body" scope-demo city="getCity(data.name)" nameprop="data.name"></div>
</div>
</body>
<script type="text/ng-template" id="scopeTemplate">
<div class="panel-body">
<p>Name:{{local}}, City:{{cityFn()}}</p>
</div>
</script>
<script>
var myApp = angular.module('myApp', [])
.controller('myCtrl', ["$scope", function ($scope) {
$scope.data = {name:"staven",defaultCity:"hefei"};
$scope.getCity = function (name) {
console.log(1);
return name == 'staven' ? $scope.data.defaultCity : "Unknown";
}
}])
.directive('scopeDemo', function () {
return {
template: function () {
return angular.element(document.querySelector("#scopeTemplate")).html();
},
scope:{
local:"=nameprop",
cityFn:"&city"
}
};
});
</script>
<div class="panel-body" scope-demo city="getCity(nameVal)" nameprop="data.name"></div>
<script type="text/ng-template" id="scopeTemplate">
<div class="panel-body">
<p>Name:{{local}}, City:{{cityFn({nameVal: local})}}</p>
</div>
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有