使用来自 DOM 的信息创建 controller/service 来保存数据

Creating a controller/service to persist data, with information from the DOM

在 angular 中,我在一个视图中有一个项目列表,这些项目将根据是否被选中显示在另一个视图中。

这是您检查要在另一个视图中显示哪些运动的视图

<ion-item ng-repeat="sport in sports"
          ng-init="sport.checked = true"
          ng-click="sport.checked = !sport.checked">
    {{:: sport.name}}
</ion-item>

这是另一个视角

<div ng-show="sport.checked"
  ng-repeat="sport in sports">
    {{sport.name}}
</div>

如您所见,所有这些运动都有一个 ng-init = sport.checked = true,因此所有运动都显示在开头,但您可以取消选中这些运动,以便在再次选中之前看不到它们。

这就是我目前所做的一切,但是一旦你刷新页面,所有的检查和取消检查都会消失,刷新后一切都会重新开始,所以你知道我需要将数据保存在数据库或其他东西中,我为此,我正在使用 node.js/sails.js 和 Redis。我知道如何在 node.js 部分创建几乎所有内容,但我在 Angular 部分需要一些控制器和服务的帮助,所以我在这里需要你的帮助。

我希望用户只能查看一次他们的运动,而不是每次他们登录应用程序时。

那么我可以向控制器添加哪些功能以在取消选中一项运动时向服务提供建议,以及服务将如何接收该数据?

此外,如果您对 NodeJS 部分有任何建议,那也很棒。

这是您可以使用的示例。这是非常简陋的。所有 $http 函数都与 promises 一起工作。控制器将从服务获取设置,如果为空,它会从服务请求新设置

angular.module('app').controller('appctrl',['appsrv','$scope',function(appsrv, $scope){
    $scope.settings = service.settings;
    if(!$scop.settings){
       //retrieve data from server when it's not in the service
       appsrv.getSettingsRemote().then($scope.settings = service.settings);
}

    }]).service('appsrv',[$http, function($http){

    return {
    getSettings :  function (){
    // get settings from localstorage
},
    setSettings :  function (){
    // store settings to localstorage
},
    getSettingsRemote : function () {

    $http.get('url').then(//set things locally);
}
}

}]);

然后从您的视图中,您可以将控制器分配给视图,然后引用您的设置,例如 {{settings.propname}}

我不想写你所有的代码,但也许这让你了解了你可以做什么