I read several articles and did not find an example that solves my problem.
I understand that:
ng-if and ng-repeat create selection areas.- Using
$parent.someProperty bad. - Using
$parent.$parent.someProperty would be an abomination.
So, with this template markup, how can I properly link the controller to update the controller property?
Markup:
(note the nested ng-if and ng-repeat , creating the form $parent.$parent )
<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 build the directive and / or markup correctly to gain access to settings.someProperty in the controller? Or is there something completely different that I need to do?
Explanation
There seems to be some kind of confusion about what I'm trying to do. someProperty is available in the directive - this works fine. Note that I am trying to assign values ββto someProperty controller someProperty from a directive (using ng-model)
Update
I revised the code above to a known working code and added jsFiddle . Note that it works, but uses $parent.$parent in the template. This is a problem that I need to understand how to solve it.
source share