使用angular.js未显示内容

时间:2016-09-28 12:54:46

标签: javascript html angularjs

<html ng-app = "sampleMod">
    <head>

    </head>

    <body ng-controller = "sampleCon">
        <script src = "bower_components/angular/angular.min.js"></script>
        <h1>Filters example</h1>
        Comments (upper) :- <input type = "text" ng-model = "sampleCon.upper"><br>
        {{sampleCon.upper | uppercase}}<br>
        Comments (lower) :- <input type = "text" ng-model = "sampleCon.lower"><br>
        {{sampleCon.lower | lowercase}}<br>
        <button type = "button">Name</button>
        <button type = "button">Age</button>
        <div>
            <ul>
                <li ng-repeat = "person in sampleCon.array1">
                    <b>Name</b> - {{person.name}}<br>
                    <b>Age</b> - {{person.age}}<br><br>
                </li>
            <ul>
        </div>

        <script>
            app = angular.module("sampleMod",[]);
            app.controller("sampleCon",function(){
                this.upper = "";
                this.lower = "";
                this.array1 = [{name:"sahib",
                              age:17},
                              {name:"kukku",
                              age:25},
                              {name:"meena",
                              age:45},
                              {name:"ayaan",
                              age:2},
                             ]
            });
        </script>
    </body>
</html>

我使用角度js创建了一个网页,一切似乎都运行正常,但屏幕上看不到具有ng-repeat指令的内容。我无法检测到上述程序中的任何错误

5 个答案:

答案 0 :(得分:1)

使用$scope变量而不是

$scope.upper = "";
$scope.lower = "";
$scope.array1 = [{name:"sahib",
                              age:17},
                              {name:"kukku",
                              age:25},
                              {name:"meena",
                              age:45},
                              {name:"ayaan",
                              age:2},
                             ]

和HTML应该是,

 <h1>Filters example</h1>
        Comments (upper) :- <input type = "text" ng-model = "upper"><br>
        {{upper | uppercase}}<br>
        Comments (lower) :- <input type = "text" ng-model = "lower"><br>
        {{lower | lowercase}}<br>
  <div>
        <ul>
            <li ng-repeat = "person in array1">
                <b>Name</b> - {{person.name}}<br>
                <b>Age</b> - {{person.age}}<br><br>
            </li>
        <ul>
    </div>

<强> DEMO

答案 1 :(得分:1)

而不是

<li ng-repeat = "person in sampleCon.array1">
                    <b>Name</b> - {{person.name}}<br>
                    <b>Age</b> - {{person.age}}<br><br>
                </li>

使用

<li ng-repeat = "person in array1">
                    <b>Name</b> - {{person.name}}<br>
                    <b>Age</b> - {{person.age}}<br><br>
                </li>

答案 2 :(得分:1)

作为other answers correctly mentioned,您可以在控制器中使用gradle代替$scope。或者,您可以使用controller as - 语法。

    

this

答案 3 :(得分:1)

除了sampleCon

之外,删除HTML中的ng-controller = "sampleCon"

sampleCon.upperupper
sampleCon.lowerlower
sampleCon.array1array1

或者只做ng-controller = "sampleCon as sampleCon"

答案 4 :(得分:0)

app.controller("sampleCon",function($scope){
     $scope.upper = "";
     $scope.lower = "";
     $scope.array1 = [{name:"sahib",
                        age:17},
                              {name:"kukku",
                              age:25},
                             ]
   }); 

使用$ scope和html,如

 <li ng-repeat = "person in array1">
                <b>Name</b> - {{person.name}}<br>
                <b>Age</b> - {{person.age}}<br><br>
            </li>