渲染后将控制器复杂数据传递给指令

时间:2018-01-12 05:25:23

标签: angularjs

刚刚开始学习角度,并且在将复杂数据从控制器传递到afterRender之后执行的指令时遇到了一些麻烦。我看过无数其他帖子,但似乎没有一个是我的确切情况。

在HTML方面:

<ul after-render="AssignSubCatExpnasion" class="inner">
<li class="titleLevelSecond" ng-repeat="SubCat1 in Categories[0].Children">
    <div class="titleLevelSecondDiv toggle">
        <span class="title"> {{ SubCat1.Name }} </span><span class="numbers">{{ SubCat1.Count }}</span>
    </div>
    <ul  class="inner">
        <li class="titleLevelThird" ng-repeat="SubCat2 in SubCat1.Children">
            <div class="titleLevelThirdDiv">
                <span class="title">{{ SubCat2.Name }}</span> <span class="numbers">{{ SubCat2.Count }}</span>
            </div>
        </li>
    </ul>
</li>

在Angular Side:

<script>
var appGetBetCategories = angular.module('BetCategories', []);
appGetBetCategories.controller('GetBetCategories', function ($scope, $http) {
    $http.get("http://localhost:49780/api/betcategory").then(function (response) {
        $scope.Categories = response.data;
    });
});
appGetBetCategories.directive('afterRender', ['$timeout', function ($timeout) {
    var def = {
        restrict: 'EA',
        terminal: true,
        transclude: false,
        scope: {
            Categories: '=',
        },
        link: function (scope, element, attrs) {
            $timeout(scope.$eval(attrs.afterRender), 0);
        }
    };
    return def;
}]);

没有指令,所有表达式都可以正常执行,但是在完成API调用并且已经渲染了控件之后,我需要运行名为“AssignSubCatExpnasion”的js函数。我知道我需要以某种方式将我的控制器范围传递给指令,因为我已经在after-render =“”内部调用了一个函数,所以无法将其包裹起来。

非常感谢任何帮助或建议。

0 个答案:

没有答案
相关问题