数据不会从角度js中的列表填充

时间:2015-07-09 11:20:08

标签: javascript angularjs

我是棱角分明的新手,我有一个问题。我的重复代码是

{foreach from=$rows item=komunikaty}
         {$komunikaty.title}
{/foreach}

我的控制器是:

SELECT table2.txt FROM table2, table1 WHERE table2.naam=table1.naam

运行此代码时,不会从列表中填充数据。任何人都可以帮我解决这个问题.... :)

4 个答案:

答案 0 :(得分:2)

在$ scope中传递值而不是$ login:

schema = Schema(All(Coerce(float), Range(min=200)))

我可以认为你用$ login替换$ scope。在angularjs中,如果你想使用$ scope而不是将其重命名为其他任何东西,变量名应该是精确的。

答案 1 :(得分:1)

几个解决方案:

  • $login应为$scope

    myApp.controller('Controller', function Controller($scope) {
      $scope.username = [{
        "name": "Dr. Sarfaraz",
        "un": "dsar@gmail.com",
        "pass": "12345212e2"
      }, {
        "name": "Dr. Abdullah",
        "un": "abdul@yahoo.com",
        "pass": "13e45212e2"
      }, {
        "name": "admin",
        "un": "admin@ehr.com",
        "pass": "*2345*12e2"
      }];
    });
    
  • ['$scope', function Controller($login){

    myApp.controller('Controller', ['$scope',
      function Controller($login) {
        $login.username = [{
          "name": "Dr. Sarfaraz",
          "un": "dsar@gmail.com",
          "pass": "12345212e2"
        }, {
          "name": "Dr. Abdullah",
          "un": "abdul@yahoo.com",
          "pass": "13e45212e2"
        }, {
          "name": "admin",
          "un": "admin@ehr.com",
          "pass": "*2345*12e2"
        }];
      }
    ]); 
    
  • 用作语法和this.username=[

    myApp.controller('Controller', function Controller() {
      this.username = [{
        "name": "Dr. Sarfaraz",
        "un": "dsar@gmail.com",
        "pass": "12345212e2"
      }, {
        "name": "Dr. Abdullah",
        "un": "abdul@yahoo.com",
        "pass": "13e45212e2"
      }, {
        "name": "admin",
        "un": "admin@ehr.com",
        "pass": "*2345*12e2"
      }];
    });
    

    并在html ng-controller="Controller as ctrl"ng-repeat="item in ctrl.username"

正如您在下一个代码段中看到的那样 - 所有解决方案都有效,如果您在控制台中看不到任何错误,那么您可能会忘记添加ng-app属性。

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

myApp.controller('Controller', function Controller($scope) {
  $scope.username = [{
    "name": "Dr. Sarfaraz",
    "un": "dsar@gmail.com",
    "pass": "12345212e2"
  }, {
    "name": "Dr. Abdullah",
    "un": "abdul@yahoo.com",
    "pass": "13e45212e2"
  }, {
    "name": "admin",
    "un": "admin@ehr.com",
    "pass": "*2345*12e2"
  }];
});

myApp.controller('Controller2', ['$scope',
  function Controller($login) {
    $login.username = [{
      "name": "Dr. Sarfaraz",
      "un": "dsar@gmail.com",
      "pass": "12345212e2"
    }, {
      "name": "Dr. Abdullah",
      "un": "abdul@yahoo.com",
      "pass": "13e45212e2"
    }, {
      "name": "admin",
      "un": "admin@ehr.com",
      "pass": "*2345*12e2"
    }];
  }
]);

myApp.controller('Controller3', function Controller() {
  this.username = [{
    "name": "Dr. Sarfaraz",
    "un": "dsar@gmail.com",
    "pass": "12345212e2"
  }, {
    "name": "Dr. Abdullah",
    "un": "abdul@yahoo.com",
    "pass": "13e45212e2"
  }, {
    "name": "admin",
    "un": "admin@ehr.com",
    "pass": "*2345*12e2"
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.js"></script>
<div ng-app="myApp">
  <div id="list" ng-controller="Controller">
    First:
    <ul class="list-group">
      <li class="list-group-item" ng-repeat="item in username">
        <div>
          <h2 class="list-group-item-heading">{{item.name}}</h2>
          <h3 class="list-group-item-heading">{{item.un}}</h3>
        </div>
      </li>
    </ul>
  </div>

  <div id="list" ng-controller="Controller2">
    Second:
    <ul class="list-group">
      <li class="list-group-item" ng-repeat="item in username">
        <div>
          <h2 class="list-group-item-heading">{{item.name}}</h2>
          <h3 class="list-group-item-heading">{{item.un}}</h3>
        </div>
      </li>
    </ul>
  </div>

  <div id="list" ng-controller="Controller3 as ctrl">
    third:
    <ul class="list-group">
      <li class="list-group-item" ng-repeat="item in ctrl.username">
        <div>
          <h2 class="list-group-item-heading">{{item.name}}</h2>
          <h3 class="list-group-item-heading">{{item.un}}</h3>
        </div>
      </li>
    </ul>
  </div>
</div>

答案 2 :(得分:0)

var app = angular.module("myApp", []);
app.controller('myController', function ($scope, $http) {
$scope.username=[{//data}]
});

答案 3 :(得分:0)

以下代码适用于我。不要忘记在div属性中设置ng-app并注入'$scope'之前用作控制器的参数。

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" id="list" ng-controller="Controller">
    <ul class="list-group">
        <li class="list-group-item" ng-repeat="item in username">
            <div>
                <h2 class="list-group-item-heading">{{item.name}}</h2>
                <h3 class="list-group-item-heading">{{item.un}}</h3>
            </div>
        </li>
    </ul>
</div>

<script>
var myApp = angular.module('myApp', []);

myApp.controller('Controller', ['$scope', function ($scope) {
    $scope.username = [{
        "name": "Dr. Sarfaraz",
        "un": "dsar@gmail.com",
        "pass": "12345212e2"
    }, {
        "name": "Dr. Abdullah",
        "un": "abdul@yahoo.com",
        "pass": "13e45212e2"
    }, {
        "name": "admin",
        "un": "admin@ehr.com",
        "pass": "*2345*12e2"
    }];
}]);

</script>

</body>
</html>