如何在子指令中计算 Angular 表达式并将其公开在父指令上
How to evaluate Angular expression in child directive and expose it on parent directive?
我有一个表示一组对象的指令,我们称之为people
。
该指令在其模板中有一个重复子指令的ng-repeat
,例如 person
,它有一个表达式属性personGreeting
,应该根据其范围进行计算。
people
和person
都使用隔离作用域。
如何设置这些指令,以便我可以公开people
指令上的personGreeting
,并在person
指令的范围内对其进行评估?
下面是一个示例:
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.myPeople = [{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Steve'
}, {
id: 3,
name: 'Joe',
}]
})
.directive('people', function() {
return {
scope: {
peopleList: '=',
eachPersonGreeting: '&'
},
template: '<ul><person ng-repeat="currentPerson in peopleList" person-greeting="eachPersonGreeting(currentPerson)"></person></ul>'
}
})
.directive('person', function() {
return {
scope: {
personDetails: '=',
personGreeting: '&'
},
template: '<li>{{personGreeting(personDetails)}}</li>'
}
})
.directive('people2', function() {
return {
scope: {
peopleList: '=',
eachPersonGreeting: '@'
},
template: '<ul><person-2 ng-repeat="currentPerson in peopleList" person-greeting="{{eachPersonGreeting}}"></person-2></ul>'
}
})
.directive('person2', function() {
return {
scope: {
personDetails: '=',
personGreeting: '@'
},
template: '<li>{{personGreeting}}</li>'
}
})
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
</head>
<body ng-controller="ctrl">
<h4>Here we are just using an in-line expression with ng-repeat, which works as you'd expect:</h4>
<ul>
<li ng-repeat="currentPerson in myPeople">Hello, {{currentPerson.name}}!</li>
</ul>
<h4>But what if we have custom directives, `people`, and `person`, and we want to let consumers of our `people` directive specify how each `person` should be greeted without making them override our directive's template, and also have data binding still work?</h4>
<h4>Unfortunately, this doesn't seem to work:</h4>
<people people-list="myPeople" each-person-greeting="'Welcome, ' + personDetails.name + '!'"></people>
<h4>Neither does this:</h4>
<people-2 people-list="myPeople" each-person-greeting="Welcome, {{personDetails.name}}!"></people-2>
</body>
</html>
我也开始深入研究这两个指令以及$interpolate服务的编译、链接和控制器函数,并让它工作起来,但它变得非常奇怪和混乱,我无法让数据绑定工作,所以感觉像是浪费精力。我觉得这应该很简单,但似乎并非如此。
有没有一种优雅的方法可以做到这一点?
问
候语如何取决于用户本身?有没有办法创建一个知道如何产生定制问候语的服务?
如果不是,那么代理对象呢? https://www.youtube.com/watch?v=AIO2Om7B83s&feature=youtu.be&t=15m1s我昨天发现了它,从我的角度来看,它似乎适合这里。您应该为每个来宾创建代理对象,将其注入来宾指令,并在链接阶段将解析的(通过角度完成)问候语放入注入的代理对象中。
另外,我认为您正在做的事情可以通过比从外部设置属性并解析它更简单的方式完成。
好的,
我设法使用与工作相同的概念将一个简单的留言簿应用程序放在一起:http://plnkr.co/edit/R7s6xE?p=info
angular.module('guestbookApp', [])
.controller('guestbookCtrl', function($scope) {
$scope.latestGuests = [{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Steve'
}, {
id: 3,
name: 'Joe',
}];
$scope.newGuest = {
name: ''
};
$scope.addGuest = function() {
$scope.latestGuests.push(angular.extend(angular.copy($scope.newGuest), {
id: $scope.latestGuests.length + 1
}));
$scope.newGuest.name = '';
};
})
.directive('guestList', function($parse) {
return {
scope: {
guests: '='
},
template: '<ul><li guest ng-repeat="currentGuest in guests | limitTo: -5" guest-details="currentGuest"></li></ul>',
controller: function($scope, $element, $attrs) {
this.greeting = function(scope) {
return $parse($attrs.greeting)(scope);
};
}
};
})
.directive('guest', function($parse) {
return {
scope: {
guestDetails: '='
},
template: '{{greeting}}',
require: '?^guestList',
link: function(scope, element, attrs, controller) {
var updateGreeting;
if (controller) {
updateGreeting = function() {
scope.greeting = controller.greeting(scope);
};
} else if (attrs.greeting) {
updateGreeting = function() {
scope.greeting = $parse(attrs.greeting)(scope);
};
}
scope.$watch('guestDetails.name', function() {
updateGreeting();
});
}
};
});
<!DOCTYPE html>
<html ng-app="guestbookApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.js"></script>
</head>
<body ng-controller="guestbookCtrl">
<h4>Latest guests:</h4>
<guest-list guests="latestGuests" greeting="'Welcome, ' + guestDetails.name + '!'"></guest-list>
<h4>Type your name below to sign the guestbook:</h4>
<input type="text" ng-model="newGuest.name" />
<button ng-click="addGuest()" ng-disabled="!newGuest.name">Sign</button>
<div ng-if="newGuest.name">
<p guest guest-details="newGuest" greeting="'Hello, ' + guestDetails.name + '! Click ''Sign'' to sign the guestbook!'"></p>
</div>
</body>
</html>
如果有人对如何改进有任何建议,请告诉我!我仍然对使用$parse
和$watch
感到有点讨厌,但也许这是不可避免的?
相关文章:
- 将JSON对象传递给angular指令
- Angular指令在alertify setContent内容中不起作用
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 向动态生成的DOM添加Angular自定义指令
- 使用指令的angular js中的Like和different
- angular type=[number]指令来阻止粘贴
- Angular Datetimepicker:指令'的模板;uib时间选择器'必须只有一个根元素
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 在其他javascript框架模板中运行angular指令
- 如何在给定的angular应用程序中获取所有指令名称
- Angular,从指令控制器中的控制器触发函数
- AngularJS:带有HTML和angular表达式的指令;编译”;具有外部范围的内容
- 将jquery插件转换为指令angular
- 基于嵌套指令Angular的宽度更改文本
- 如何将函数添加到禁用的指令 - Angular 2
- 我在从我的指令 Angular 访问控制器的范围时遇到问题
- 如何将变量从控制器传递到指令?Angular.JS
- 单指令Angular JS中的可选/多模板URL
- 调用链接函数指令Angular 1 ES6
- 指令angular中的访问范围控制器变量