$ route.reload不更新服务数据

时间:2018-03-15 06:54:11

标签: angularjs service controller routes

我对AngularJS非常陌生 通常我看到有人使用$route来刷新数据库中的数据。但出于学习目的,我在自定义服务中创建了一个对象,将一些数据存储在Object中并在控制器中调用该服务。

但是当我在对象中添加更多值并尝试使用ngClick更新它时,不会发生任何更改。但是,在刷新整个页面时,会发生变化。但我只是希望更新特定部分。

这是我的JavaScript代码:

var app=angular
        .module('app',['ngRoute'])
        .service('data', function(){

            this.students=[
            {id: 1, name: 'Mark', gender: 'Male', city: 'London'},
            {id: 2, name: 'Henry', gender: 'Male', city: 'Manchester'},
            {id: 3, name: 'John', gender: 'Male', city: 'Chennai'},
            {id: 4, name: 'Sara', gender: 'Female', city: 'Sydney'},
            {id: 5, name: 'Tom', gender: 'Male', city: 'New York'},
            {id: 6, name: 'Pam', gender: 'Male', city: 'Los Angeles'},
            {id: 7, name: 'Catherine', gender: 'Female', city: 'Chicago'},
            {id: 8, name: 'Mary', gender: 'Female', city: 'Houston'},
            {id: 9, name: 'Mike', gender: 'Male', city: 'Phoenix'},
            {id: 10, name: 'Rosie', gender: 'Female', city: 'Manchester'},
            {id: 11, name: 'Sasha', gender: 'Female', city: 'Hyderabad'},
            {id: 12, name: 'Naatasha', gender: 'Female', city: 'Bhungi'},
            // {id: 13, name: 'akash', gender: 'Male', city: 'lugi'},
            // {id: 14, name: 'aaakash', gender: 'Male', city: 'lugi'}
            ];

        })

        .controller('studentcontroller',function($scope,data,$route){
            $scope.rdata=function(){
                console.log("asd");
                $route.reload();
            };  

            $scope.students=data.students;

        });


以下是调用它的代码

<h1>List of Students</h1>
<ul>
    <li ng-repeat="student in students">
        <a href="students/{{student.id}}">{{student.name}}</a>
    </li>
</ul>

<button ng-click="rdata()">Reload Data</button>



请帮助我学习新的东西。

0 个答案:

没有答案