Делается это через создание новой директивы, которая ссылается на родительский scope, при этом например можно функцию сохранения слинковать от родителя. Вообщем у меня вышло вот так:
| Код | app.controller("Objections", function($scope, $filter, $routeParams, theService) { $scope.oId = $routeParams.oId || false; console.log($scope.oId) $scope.objections = theService.data.objections; $scope.questions = theService.data.questions; $scope.objection = $scope.objections._find("id", $scope.qId) || { id: "", text: "", questions: [] } $scope.delete = function() { if ($scope.oId === false) return false; $scope.objections.splice($scope.objections._find("id", $scope.oId, true), 1); $scope.oId = false; } $scope.save = function() { if (!$scope.model.objection) return false; } $scope.cancel = function() { $scope.model = angular.copy($scope.initial); } $scope.add = function() { $scope.objections.push({ id: $scope.objections[$scope.objections.length-1].id, text: $scope.objection.text })
} $scope.$watch("oId", function(newValue, oldValue) { if (newValue===undefined || newValue===false) return $scope.objection = { id: "", text: "", questions: [] } $scope.objection = $scope.objections._find("id", newValue) if (!$scope.objection) return false; $scope.objection.questions = $scope.objection.question_list.length>0 ? $scope.objection.question_list.map(function(obj1,i1) { return theService.data.questions.filter(function(obj2, i2) { return obj2.id==obj1 } )[0] }).filter(function(obj){ return obj }) : []; }) })
app.directive("ngShadow", function() { return { restrict: "A", scope: { data: "=ngData" }, link: function($scope, $element, $attrs) { $scope[$attrs["ngData"]] = $scope.data; $scope.save = function(func) { return function() { func($scope.data) } }($scope.save) } } })
|
| Код | <script type="text/ng-template" id="objections.html"> <div class="stroke"> <label>Поиск: <input class="border" type="text" ng-model="search.text"></label> <ul class="objections-objections left_column"> <li class="border" ng-repeat="objection in objections | filter:search"> <a href="#/objections/{{objection.id}}" ng-shadow="shadow" ng-data="objection"> {{objection.text}} </a> </li> </ul> <div class="right_column"> <p class="header"></p> <p class="text"> <span>ID возражения: {{objection.id}}</span> <br> <textarea class="border" name="" id="" cols="30" rows="10" ng-model="objection.text"></textarea> </p> <ul class="questions-objections" ng-show="objection.questions.length"> <span>Список вопросов</span> <li class="border" ng-repeat="question in objection.questions"> <a href="#/questions/{{question.id}}">{{question.text}}</a> </li> </ul> <p class="footer"> <button ng-click="add()">Добавить</button> <a href="" ng-click="delete(objection)">Удалить</a> <a href="" class="save" ng-click="save()">Сохранить</a> <a href="" class="close" ng-click="cancel()">Закрыть</a> </p> </div> </div> </script>
|
|