2.4.1 AngularJS自定义指令 scope参数等 - OhNaNaSun/angularBlog GitHub Wiki
<div ng-app="myApp">
<div ng-controller="helloCtrl">
<hello-world saywhat="sayhello()"></hello-world>
</div>
<div ng-controller="shitCtrl">
<hello-world saywhat = "sayshit()"></hello-world>
</div>
</div>
var myApp = angular.module("myApp", []);
myApp
.controller("helloCtrl", function($scope) {
$scope.sayhello = function(){
console.log("hello")
}
})
.controller("shitCtrl", function($scope){
$scope.sayshit = function(){
console.log("shit")
}
})
.directive("helloWorld", function() {
return {
restrict: "AEC",
replace: true,
template: "<div><h3>hello or shit?</h3></div>",
link: function(scope, elem, attr){
elem.bind("mouseenter", function(){
scope.$apply(attr.saywhat);
})
}
}
})
父级指令的controller暴露方法。子级通过link的第四个参数调用这个方法。
<div ng-app="myApp">
<div ng-controller="myCtrl">
<super strength>strength</super><br/>
<super strength speed>strength+speed</super><br/>
</div>
</div>
var myApp = angular.module("myApp", []);
myApp
.controller("myCtrl", function($scope) {
})
.directive("super", function(){
return {
scope: {},
restrict: "E",
controller: function($scope){
$scope.abi = []
this.addbis = function(b){
$scope.abi.push(b);
}
},
replace: true,
link: function(scope, elem, attr){
elem.bind("mouseenter", function(){
console.log(scope.abi);
})
}
}
})
.directive("strength", function() {
return {
require: "^super",
link: function(scope, elem, attr, superCtrl){
superCtrl.addbis("strength");
}
}
})
.directive("speed", function() {
return {
require: "^super",
link: function(scope, elem, attr, superCtrl){
superCtrl.addbis("speed");
}
}
})
单向绑定。获取父controller上的值。用link也可以实现。
<div ng-app="myApp">
<div ng-controller="helloCtrl">
<hello flavor = "{{ctrlFlavor}}">
</hello>
</div>
</div>
var myApp = angular.module("myApp", []);
myApp
.controller("helloCtrl", function($scope) {
$scope.ctrlFlavor = "sss";
})
.directive("hello", function() {
return {
restrict: "AEC",
replace: true,
template: "<div>{{flavor}}</div>",
//link: function(scope, elem , attr){
// scope.flavor = attr.flavor
//}
scope: {
flavor: "@"
}
}
})
<div ng-app="myApp">
<div ng-controller="helloCtrl">
<input type="text" ng-model="ctrlFlavor">
<hello flavor = "ctrlFlavor">
</hello>
</div>
</div>
var myApp = angular.module("myApp", []);
myApp
.controller("helloCtrl", function($scope) {
$scope.ctrlFlavor = "sss";
})
.directive("hello", function() {
return {
restrict: "AEC",
replace: true,
template: "<div><input type='text' ng-model='flavor'/></div>",
scope: {
flavor: "="
}
}
})
可以用1中的
<div ng-app="myApp">
<div ng-controller="helloCtrl">
<hello-world say="saywhat(name)"></hello-world>
<hello-world say = "saywhat(name)"></hello-world>
</div>
</div>
var myApp = angular.module("myApp", []);
myApp
.controller("helloCtrl", function($scope) {
$scope.saywhat = function(name){
console.log(name)
}
})
.directive("helloWorld", function() {
return {
restrict: "AEC",
replace: true,
scope: {say: "&"},
template: "<div><input type='text' ng-model='username'/>"+
"<button ng-click='say({name:username})'></button></div>",
link: function(scope, elem, attr){
}
}
})