Angularjs 访问子作用域
我正在使用ngTagsInput。我的问题来自尝试从父根作用域访问displayTags。它不在那里。我想访问它,因为用户可以向displayTags添加新的标签,而displayTags工作得非常好。我想把这些新标签贴上去。如何在循环之外的$root中访问displayTags 像这样的角度代码:Angularjs 访问子作用域,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正在使用ngTagsInput。我的问题来自尝试从父根作用域访问displayTags。它不在那里。我想访问它,因为用户可以向displayTags添加新的标签,而displayTags工作得非常好。我想把这些新标签贴上去。如何在循环之外的$root中访问displayTags 像这样的角度代码: var app = angular.module('recGroups', ['ngTagsInput']); app.directive('group', function () { r
var app = angular.module('recGroups', ['ngTagsInput']);
app.directive('group', function () {
return {
restrict: 'E',
scope: true,
templateUrl: '../../Content/templates/RecruitingGroups/Group.html',
link: function (scope, el) {
//RemoveGroup
scope.removeGroup = function (groupArray, index) {
groupArray.splice(index, 1);
}
}
}
});
app.directive('rule', function () {
return {
restrict: 'E',
scope: true,
templateUrl: '../../Content/templates/RecruitingGroups/Rule.html',
link: function (scope, el) {
scope.removeRule = function (ruleArray, index) {
ruleArray.splice(index, 1);
}
scope.addTag = function () {
scope.$parent.rule.Rules.push({});
}
}
}
});
app.directive('tag', function () {
return {
restrict: 'E',
scope: true,
templateUrl: '../../Content/templates/RecruitingGroups/Tag.html',
controller: 'displayTags',
link: function (scope, el) {
scope.removeTag = function (tagArray, index) {
tagArray.splice(index, 1);
}
}
}
});
app.controller('displayTags', function ($scope, data) {
$scope.displayTags = [];
// tag stuff to manipulate when have proper tag model
if ($scope.tag.TagIds != null) {
$.each($scope.tag.TagIds, function (scopeIndex, scopeValue) {
$.each(data.Project.Tags, function (tagIndex, tagValue) {
if (tagValue.Id == scopeValue) {
$scope.displayTags.push({ Id: tagValue.Id, Name: tagValue.Name, ProjectId: tagValue.ProjectId });
}
});
});
}
});
我还将此图像作为html的视觉显示提交:
Html简化如下:
<group ng-repeat="group in groups">
<rule ng-repeat="rule in group.Rule.Rules">
<tag ng-repeat="tag in rule.Rules">
<tags-input ng-model="displayTags" display-property="Name">
</tags-input>
</tag>
</rule>
</group>
您可以使用下面的示例,而不是使用
scope:true
scope: {
displayTags: '=' // Bi-directional binding
}
或者,如果displayTags
是您的model
(或父^model
),您可以使用
嗯,我尝试了第一种方法,但失败了,抛出了这个错误:TypeError:无法读取未定义的属性“tagid”。我有点明白了。我只是完成了对displaytags的绑定,然后让它绑定到TagID。这几乎给了我我想要的东西。
app.directive('tag', function () {
return {
restrict: 'E',
scope: true,
require: "ngModel",
templateUrl: '../../Content/templates/RecruitingGroups/Tag.html',
link: function ( scope, el, attrs, ngModelCtrl ) {
scope.doSomething = function() {
ngModelCtrl.$setViewValue( 'testing' ); // Update model
}
}
}
});