【问题标题】:$scope not working, content not populating$scope 不工作,内容不填充
【发布时间】:2015-04-08 15:00:37
【问题描述】:

我已经看了好几天了,不明白为什么我的内容没有填充我页面上的胡须!我得到的唯一错误消息是 $scope 没有定义,但我不确定问题出在哪里。任何帮助将不胜感激 - 谢谢!

itemlistcontroller.js 文件:

vegApp.controller('itemListController', function ($scope){
$scope.items = [
        {imageurl: 'images/strawb.jpg', type: 'strawberry', name: 'Herbert     Strawberry', occupation: 'dogwalker', superpower: 'power-C boost'},
        {imageurl: 'images/blueb.jpg', type: 'blueberry', name: 'Ulysses  Blueberry', occupation: 'construction worker', superpower: 'super strength' },
        {imageurl: 'images/orange.jpg' , type: 'orange', name: 'Otto Sly Orange', occupation: 'ninja', superpower: 'serious defense' }
    ]});
    $scope.new_item = {};
$scope.add_item_error = "";

$scope.additem = function(item){
        $scope.items.push( item );
        $scope.adding_item = {};
    };

    console.log('scope', $scope);
]);

app.js 文件:

var vegApp = angular.module('vegApp', []);

vegApp.config(['$routeProvider', function($routeProvider){

    $routeProvider
    .when('/', {
        templateUrl: 'index3.html',
        controller: 'itemListController'
    })
    .when('/login', {
        templateUrl: 'partials/login.html',
        controller: 'registration'
    })
    .when('/moreinfo', {
        templateUrl: 'partials/moreinfo.html',
        controller: 'moreinfo'
    })
    .when('/recipes', {
        templateUrl: 'recipes.html',
        controller: 'recipes'
    })
    .otherwise({
        redirectTo: '/login'
    });
}
]);

HTML 文件:

<html ngApp="vegApp">
<head>
<link rel="stylesheet" text="text/css" href="css/bootstrap.css">
<link rel="stylesheet" text="text/css" href="css/style.css">
<script type="text/javascript" src="angular/js/angular.js"></script>
<script src="text/javascript" src="angular/js/angular-route.js"></script>
  <script type="text/javascript" src="app/app.js"></script>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

</head>
<body ng-controller='itemListController'>

<ng-view>


<div class="main">
  <h2>Summon your Vitamin Power</h2>
  <form name="add_item_form" novalidate ng-submit="additem()">
    <input type="text" placeholder="what'd you eat?" ng-model="searchFor" ng-required="true"/>

    <button ng-click="additem()" ng-disabled="add_item_form.$invalid" class="btn btn-success">Add to Your Arsenal</button>

    <ul class = "itemgroup" ng-repeat= "item in items | filter:searchFor">
      <div class="adding_item">
        <li ng-model="items.imageurl">
          <img ng-src ="{{ item.imageurl }}" alt="{{ item.type }}">
        </li>
        <li ng-model="items.type">
          {{ item.type }}
        </li>
        <li ng-model="items.name">
          {{ item.name }}
        </li>
        <li ng-model="items.occupation" >
          {{ item.occupation }}
        </li>
        <li ng-model="items.superpower">
          {{ item.superpower }}
        </li>
      </div>
    </ul>

  </form>

</div>

</div>
</ng-view>
</body>
</html>

【问题讨论】:

  • 如果您包含错误消息可能会有所帮助,而不仅仅是“未定义$scope”。
  • ng-app="vegApp" 不是ngApp="vegApp"

标签: angularjs-scope ngroute


【解决方案1】:

更新:

似乎在您的 HTML 中,您将列表项的 ng-model 设置为“items.”。这没有任何意义,因为例如 $scope.items 中没有属性“类型”;它是一个数组。相反,由于 'item' 已经是对范围内项目的引用,因此您可以简单地编写 ng-model="item"。在您的 HTML 中。

您的 itemListController 看起来有点畸形。您的函数在您定义 $scope.items 后立即结束,因此 $scope 不在您控制台记录 $scope 的范围内。尝试用这个替换你的 itemlistcontroller.js:

vegApp.controller('itemListController', function ($scope) {
    $scope.items = [
        {
            imageurl: 'images/strawb.jpg',
            type: 'strawberry',
            name: 'Herbert     Strawberry',
            occupation: 'dogwalker',
            superpower: 'power-C boost'
        },
        {
            imageurl: 'images/blueb.jpg',
            type: 'blueberry',
            name: 'Ulysses  Blueberry',
            occupation: 'construction worker',
            superpower: 'super strength'
        },
        {
            imageurl: 'images/orange.jpg',
            type: 'orange',
            name: 'Otto Sly Orange',
            occupation: 'ninja',
            superpower: 'serious defense'
        }
    ];
    $scope.new_item = {};
    $scope.add_item_error = "";

    $scope.additem = function (item) {
        $scope.items.push(item);
        $scope.adding_item = {};
    };

    console.log('scope', $scope);
});

【讨论】:

  • 谢谢——不过,内容仍然没有填充胡须。虽然现在没有错误信息....
  • 嘿,我更新了答案。看来您的 ng-model 指向了错误的东西。
  • 谢谢!所以不是
  • 或
  • ?我可以对他们都说
  • 吗?我试过了,还是不行。
  • 不,你会写
  • 和
  • 。但是,在您的标记中,您编写了
  • 和
  • 。注意“item”后面的“s”。
  • 是的,我试过了,我在页面上看到的仍然是 {{item.type}} {{item.name}} 等等。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签