我正在考虑将数据从我的控制器传递到指令模板。我怎么能这样做?
这是我的控制器:
$scope.$on('itemSelectedEvent', function (event, args) {
$scope.displayOrgs.push(data.orgName);
})
指令:
directive('search', function ($timeout) {
return {
restrict: 'AEC',
scope: {
selecteditemslist: "=",
searchid: "@",
selecteditemsdisplay: "=",
searchobj: '@',
displayOrgs: '=displayOrgs'
},
link: function (scope, elem, attrs, index, searchobj,searchid) {
scope.$emit('itemSelectedEvent', {
selectedId:scope.selectedId,
sortOrder:index,
searchId : searchid
}
);
}
});
此displayOrgs
应传递给指令。我是如何得到未定义的?
<li class="tag" ng-repeat="list in displayOrgs track by $index" ng-class="{selected: $index==selectedIndex}" >
<span class="tag-label">{{list}}</span>
<span class="tag-cross pointer" ng-click="Delete($index,selecteditemslist[$index],list,searchobj)">x</span>
</li>
请帮帮我。提前谢谢。
答案 0 :(得分:1)
无需生成事件scope.$emit
来通知控制器变量已更改。虽然您可以通过其隔离scope: { .. }
displayOrgs: '=displayOrgs'
=
表示与指令相关联的变量的双向绑定 元素属性div。
当您在指令中使用displayOrgs: '=displayOrgs'
(在隔离范围内)时,隔离范围中提到的任何变量的更改将自动更改,将这些更改应用于范围变量,即与display-orgs
属性相关联。
link: function (scope, elem, attrs) {
scope.displayOrgs.push(data.orgName); // this will update binding with parent scope.
});