AngularJS,NG-重复与NG-包括不渲染AngularJS、NG

2023-09-13 05:14:26 作者:飘児づ

我开始学习角度和我遇到一个问题,当我用NG重复的组合与NG-包括。无论我做什么我不能得到的模板来渲染。我有一个简单的控制器创建工作区的列表和每个工作空间具有TemplateUrl属性。

我知道的值是正确的,因为如果我只是呈现出原始文本{{workspace.TemplateUrl}},然后把直接到NG-包括它的工作原理没有问题。它只是似乎从来没有当它从控制器来上班。我试图把模板路径在这样的行情太多,但它没有什么区别。

  $ scope.Workspaces.push(新的工作空间('/应用/模板/ Template1.html',工作空间1)); 

当我运行它,我看到没有要求拉模板。任何人都可以在此帮助其驾驶我有点坚果。

 <!DOCTYPE HTML>< HTML NG-应用>< HEAD>    <标题>工作区< /标题>    &所述;! - 引导 - >    <链接HREF =CSS / bootstrap.css的rel =stylesheet属性的媒体=屏幕>    <脚本SRC =应用程序/库/ jquery.js和>< / SCRIPT>    <脚本SRC =应用程序/库/ bootstrap.min.js>< / SCRIPT>    <脚本SRC =应用程序/库/ angular.js>< / SCRIPT>    <脚本SRC =应用程序/ app.js>< / SCRIPT>< /头><机身NG控制器=HostController>    < D​​IV>        < UL类=无样式>            <李NG重复=工作区工作区中的>                &所述p为H.; {{workspace.TemplateUrl}}&下; / P>                <小时/>                < D​​IV NG-包括={{workspace.TemplateUrl}}>< / DIV>            < /李>        < / UL>    < / DIV>< /身体GT;< / HTML> 

控制器code

  VAR工作空间=(函数(){        功能工作区(templateUrl,名){            this.TemplateUrl = templateUrl;            this.Name =名称;        }        返回工作空间;    })();    功能HostController($范围){        $ scope.Workspaces =新的Array();        $ scope.Workspaces.push(新的工作空间(/应用程序/模板/ Template1.html,工作空间1));        $ scope.Workspaces.push(新的工作空间(/应用程序/模板/ Template2.html,工作区2));        $ scope.Workspaces.push(新的工作空间(/应用程序/模板/ Template1.html,工作区3));        $ scope.Workspaces.push(新的工作空间(/应用程序/模板/ Template2.html,工作区4));        $ scope.Workspaces.push(新的工作空间(/应用程序/模板/ Template1.html,工作区5));    } 
angularjs在ng repeat中使用ng model遇到的问题

解决方案

我引用您的code

 < D​​IV NG-包括={{workspace.TemplateUrl}}>< / DIV> 

 < D​​IV NG-包括=workspace.TemplateUrl>< / DIV> 

因为它是一个NG声明。

Hi I am starting to learn angular and I'm running into a problem when I use a combination of ng-repeat with ng-include. No matter what I do I cannot get the templates to render. I have a simple controller that creates a list of workspaces and each workspace has a TemplateUrl property.

I know the value is correct because if I just render out the raw text {{workspace.TemplateUrl}} and then put that directly into the ng-include it works no problem. It just never seems to work when its coming from the controller. I have tried putting the template path in quotes like this too but it makes no difference.

$scope.Workspaces.push(new Workspace("'/app/templates/Template1.html'", "Workspace 1"));

When I run it I am seeing no requests to pull the template. Can anyone help on this as its driving me a little nuts.

<!DOCTYPE html>
<html ng-app>
<head>
    <title>Workspaces</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet" media="screen">
    <script src="app/libraries/jquery.js"></script>
    <script src="app/libraries/bootstrap.min.js"></script>
    <script src="app/libraries/angular.js"></script>
    <script src="app/app.js"></script>
</head>

<body ng-controller="HostController">
    <div>
        <ul class="unstyled">
            <li ng-repeat="workspace in Workspaces">
                <p>{{workspace.TemplateUrl}}</p>
                <hr />
                <div ng-include="{{workspace.TemplateUrl}}"></div>
            </li>
        </ul>
    </div>

</body>
</html>

Controller code

 var Workspace = (function () {
        function Workspace(templateUrl, name) {
            this.TemplateUrl = templateUrl;
            this.Name = name;
        }
        return Workspace;
    })();
    function HostController($scope) {
        $scope.Workspaces = new Array();
        $scope.Workspaces.push(new Workspace("/app/templates/Template1.html", "Workspace 1"));
        $scope.Workspaces.push(new Workspace("/app/templates/Template2.html", "Workspace 2"));
        $scope.Workspaces.push(new Workspace("/app/templates/Template1.html", "Workspace 3"));
        $scope.Workspaces.push(new Workspace("/app/templates/Template2.html", "Workspace 4"));
        $scope.Workspaces.push(new Workspace("/app/templates/Template1.html", "Workspace 5"));
    }

解决方案

I quote Your code

 <div ng-include="{{workspace.TemplateUrl}}"></div>

should be

 <div ng-include="workspace.TemplateUrl"></div>

since it is a ng statement.