I've been reading a few articles, and haven't found the example that solves my issue.
My understanding is that:
ng-if
andng-repeat
create isolate scopes.- Using
$parent.someProperty
is bad. - Using
$parent.$parent.someProperty
would be an abomination.
So, with the given template markup, how can I properly bind the the controller so that the controller property updates?
Markup:
(note the nested ng-if
and ng-repeat
, creating a sort of $parent.$parent
situation)
<div ng-app="MyApp" ng-controller="MyCtrl">
<div ng-if="showOnCondition">
<label ng-repeat="item in repeatingItems">
{{item}}
<setting item="item" />
</label>
</div>
{{checkSetting()}}
</div>
JavaScript
var myApp = angular.module('MyApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.settings = {
someProperty: '',
anotherProperty: 'Hello'
}
$scope.repeatingItems = [
'One',
'Two',
'Three'
];
$scope.showOnCondition = true;
$scope.checkSetting = function() {
// When calling checkSettings(), I would like to access the value of someProperty here
return $scope.settings;
}
});
myApp.directive('setting', function() {
return {
restrict: 'E',
require: '^myCtrl',
// HOW DO I SOLVE THIS?
// $parent.$parent is bad.
template: '<input type="radio" ng-model="$parent.$parent.settings.someProperty" name="mySetting" value="{{item}}" />',
scope: {
settings: '=',
item: '='
}
};
});
Given the above example, how to I properly construct the directive and / or markup in order to access settings.someProperty
in the controller? Or is there something entirely different I need to be doing?
Clarification
There seems to be some confusion around what I'm trying to do. The someProperty
is available in the directive - that is working fine. Please note I'm trying to assign values to the controller's someProperty
property from within the directive (using ng-model)
Update
I've revised the code above to known, working code, plus added a jsFiddle.
Note that it works, but it uses $parent.$parent
in the template. This is the problem I need to understand how to solve.