Angularjs模块工厂服务问题

时间:2013-12-06 06:55:44

标签: javascript angularjs angularjs-module

我写了一个简单的工厂服务。 这里是小提琴的链接 http://jsfiddle.net/#&togetherjs=caUQB0merH 我无法确定为什么小提琴不起作用。任何人都可以指出问题

提前致谢

更新: -

  <script>
                var StudentModule=angular.module("StudentModule",[]);
              function  factoryfuc()
                {
                    return[
                            {name:"maclean", age:"22"},
                            {name:"sachin", age:"25"}
                        ];  

                }
                StudentModule.factory('Studentfactory',factoryfuc);

                function StudentController($scope,Studentfactory)
                {
            $scope.students=Studentfactory;

                }
                </script>
            <div ng-app="StudenModule" ng-controller="StudentController">
                <table ng-repeat="student in students"><tr><td>{{student.name}}</td><td>{{student.age}}</td></tr></table>
            </div>

1 个答案:

答案 0 :(得分:3)

我使用的是Angular的最新版本,这很有效:http://jsbin.com/eQIVuQi/1/edit

我将在本文中包含的代码是您修改的代码,以遵循正确的命名约定和更好的实践。

<强>标记:

<div ng-app="studentModule" ng-controller="studentCtrl">
  <table ng-repeat="student in students">
    <tr><td>{{student.name}}</td><td>{{student.age}}</td></tr>
  </table>
</div>

<强> JavaScript的:

var studentModule = angular.module("studentModule",[]);

studentModule.factory('studentService',function() {
  return [
    {name:"maclean", age:"22"},
    {name:"sachin", age:"25"}
  ];  
});

studentModule.controller('studentCtrl', function($scope, studentService) {
  $scope.students = studentService;
});

Live demo here (click).