【发布时间】:2014-05-02 21:18:43
【问题描述】:
我有两个 Angular 指令,一个嵌套在另一个内部,如下所示。
HTML
<gss-response-group response-group-id="43" response-group-title="'Group Title'">
<gss-response-option option-id="5" option-text="'Very Often'" option-value="5.00" options-inline="true" type="'radio'"></gss-response-option>
<gss-response-option option-id="6" option-text="'Often'" option-value="4.00" options-inline="true" type="'radio'"></gss-response-option>
<gss-response-option option-id="7" option-text="'Sometimes'" option-value="3.00" options-inline="true" type="'radio'"></gss-response-option>
<gss-response-option option-id="8" option-text="'Rarely'" option-value="2.00" options-inline="true" type="'radio'"></gss-response-option>
<gss-response-option option-id="9" option-text="'Never'" option-value="1.00" options-inline="true" type="'radio'"></gss-response-option>
</gss-response-group>
responseGroup.template.html
<div class="gss-response-group">
<label class="gss-response-group-title" ng-if="responseGroupTitle != null">{{responseGroupTitle}}</label>
<placeholder></placeholder>
</div>
responseOption.template.html
<div ng-class="{'gss-response-option': optionsInline}">
<input class="gss-{{type}}-option" id="{{id}}" name="{{groupName}}" type="{{type}}" value="{{optionValue}}" />
<label class="gss-option-text" for="{{id}}">{{optionText}}</label>
<div class="gss-specifyanswer" ng-if="specify">
<label class="gss-specifyanswer" ng-if="specifyText != null">{{specifyText}}</label>
<textarea class="gss-specifyanswer" maxlength="5000" disabled></textarea>
</div>
</div>
JavaScript
'use strict';
angular.module( 'gssApp', [ 'gss.directives' ] );
angular.module( 'gssApp' )
.controller( 'gssAppController', [
'$scope',
'$http',
'$rootScope',
function ( $scope, $http, $rootScope )
{
}] );
var directiveModule = angular.module( 'gss.directives', [] );
directiveModule.directive( 'gssResponseGroup', function ()
{
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
responseGroupTitle: '=',
responseGroupId: '='
},
templateUrl: 'responseGroup.template.html',
link: function ( scope, element, attrs, ctrl, transclude )
{
element.find( 'placeholder' ).replaceWith( transclude() );
},
controller: [
'$scope',
'$rootScope',
'$element',
'$attrs',
function ( $scope, $rootScope, $element, $attrs )
{
}]
};
} );
directiveModule.directive( 'gssResponseOption', function ()
{
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
responseGroupId: '=',
optionId: '=',
optionText: '=',
optionValue: '=',
type: '=',
optionsInline: '=',
specify: '=',
specifyText: '='
},
templateUrl: 'responseOption.template.html',
controller: [
'$scope',
'$rootScope',
'$element',
'$attrs',
function ( $scope, $rootScope, $element, $attrs )
{
// HOW DO I GET THE PARENT DIRECTIVE'S SCOPE TO USE THE
// responseGroupId FROM IT?
$scope.id = 'rgID' + '_' + $scope.responseGroupId + '_' + $scope.optionId;
$scope.groupName = 'rg' + '_' + $scope.responseGroupId;
}]
};
} );
我希望子指令能够访问父指令中的“response-group-id”字段。我该怎么做呢?通过让子指令需要父指令,然后在子指令的“链接”方法中获取它,我能够获得该值,但到那时应用到子模板为时已晚。
另外,如果有人能告诉我为什么我的 Plunker 项目中没有应用 CSS,我将不胜感激(虽然没什么大不了的)。
【问题讨论】:
-
... 为什么需要内部指令?您不能将输入定义存储在一个对象中,然后使用 ng-repeat 对其进行迭代吗?
-
如果你必须知道,应该有一个
$parent对象附加到你的范围 -
我需要内部指令用于其他目的。这只是一个精简的示例,只是为了解决我遇到的问题的核心。
-
你愿意详细说明我应该把 $parent 对象放在哪里吗?我已经在孩子内部尝试过 $scope.$parent 但它没有返回父指令。
标签: angularjs angularjs-directive angularjs-scope