嵌套指令未显示.AngularJS.

Nested Directives not showing. AngularJS

本文关键字:AngularJS 显示 指令 嵌套      更新时间:2023-09-26

我正在创建一个菜单,根据视图,它将显示正确的链接。因此,从下面的代码中,当父范围startCtrl$scope.viewActions值更改时,它将用于选择正确的菜单模板,该模板将显示在菜单侧边栏上。

我遇到的问题是我的嵌套指令模板没有显示在mobile-side-bar指令上。此外,我得到一个:

错误:[$parse:语法] 语法错误:表达式 [{{playerSearchSpinnerOn}}] 第 2 列处的标记"{"键无效,从 [{playerSearchSpinnerOn}}] 开始

当尝试绑定此 scope.playerSearchSpinnerOn,这是一个子范围,用于"startCtrl"到我的

  <advertise-game playersearchspinneron={{playerSearchSpinnerOn}}></advertise-game>

范围层次结构:

StartCtrl(根控制器)

  • (移动侧栏指令) (指令 - 独立范围)

  • AddUsersController(controller) (通过 ng-view 显示视图

然后是移动侧栏指令的嵌套指令

法典

启动控制

  monopolyMenuModule.controller('StartCtrl', ['$scope', 'startMenuServices','startMenuHeaderBar', function ($scope, services,startMenuHeaderBar,viewNamesEnum) {
    $scope.viewActions = "";
 }]);

添加用户Ctrl

monopolyMenuModule.controller('AddUsersCtrl', ['$scope', 'addUserServices', 'GameGroupDetails', 'viewNamesEnum', function ($scope, service, GameGroupDetails, viewNamesEnum) {
     // add code to call notifyUsers object.. watch pluralsight "connecting our server to client" and "how signalr works"
     $scope.playerSearchSpinnerOn = false;
     $scope.$parent.viewActions = "addUsers.html";
}]);

移动边栏指令

monopolyMenuModule.directive('mobileSideBar', function () {
    return {
        restrict: "E",
        transclude: true,
        scope: {
        },
        controller: function ($scope, menuService) {
            // code here
        },
        templateUrl: '/Js/MonopolyMenu/mobileSideBar.html'
    }
});

广告游戏指令

monopolyMenuModule.directive('advertiseGame', ['GameGroupDetails', function (GameGroupDetails) {
    return {
        restrict: "E",
        scope: {
            playerSearchSpinnerOn: "=",
        },
        template: "<div ng-click='advertiseGame()>Advertise Game</div>",
        controller: function ($scope) {
            $scope.advertiseGame = function () {
                if (GameGroupDetails != null) {
                    service.FindUsers(GameGroupDetails).done(function () {
                        // add spinner once group has been show in invite screen
                        // apply is needed and apply is only called in angularjs directives
                        $scope.$apply(function () {
                            $scope.playersearchspinneron = true;
                        });
                    });
                }
            };
        }
    }
}]);

目录

<div id="mainContainer" ng-controller="StartCtrl">
<div id="menuContainer">
    <mobile-side-bar id="menu" class="hideElement">
        <div ng-include src="viewActions"></div>
    </mobile-side-bar>
</div>
// AddUsers.html comes from $routeProvider and binding addUsersCtrl 
<div ng-view class="view-animate"></div>  
</div>

<script type="text/ng-template" id="addUsers.html">
  <advertise-game player-search-spinner-on="playerSearchSpinnerOn">  </advertise-game>
  <invite-friend></invite-friend>
  <player-search></player-search>
</script>

如何让广告游戏和我的其他嵌套指令在mobile-side-bar指令中显示其模板?

若要将值从父控制器传递到带有"="的嵌套指令,请不要使用插值。请参阅此问题的答案。

另外,请注意骆驼和连字符大小写之间的切换。通常js中的变量使用驼峰,而html属性使用连字符。

我建议将指令调用更改为:

<advertise-game player-search-spinner-on="playerSearchSpinnerOn"></advertise-game>

并且指令定义应该在scope参数中具有驼峰大小写。

scope: {
    playerSearchSpinnerOn: '='
}