简体   繁体   English

AngularJS 服务 - 在视图/控制器之间传递 object

[英]AngularJS Services - Pass object between views/controllers

I'm struggling to understand the concept of AngularJS services, I'm relatively new to AngularJS.我很难理解 AngularJS 服务的概念,我对 AngularJS 比较陌生。

I'm essentially trying to pass a nested object between two controllers/views.我本质上是在尝试在两个控制器/视图之间传递嵌套的 object。

So I have this section in my home page partial that will have the project and task.所以我在我的主页部分有这个部分,其中包含项目和任务。

homePage.html主页.html

<div class="col-8" ng-repeat = "item in toDoList">
    <h4>{{item.project}}</h4>
    <p>{{item.task}}.</p>
</div>

Then I have this section from the new item partial, which is where the new to do task is added.然后我从新项目部分中获得了这一部分,这是添加新待办任务的地方。

newitem.html新项目.html

<form class="form" ng-submit="addNewToDoTask()">
    <div class="row projectInput text-center">
        <div class="col-12">
            <input type="text" ng-model="projectInput"
                   placeholder="Enter a project title">
        </div>
    </div>
    <div class="row taskInput text-center">
        <div class="col-12">
            <input type="text" ng-model="taskInput"
                   placeholder="Enter your task details">
        </div>
    </div>

    <button type="submit" class="btn-lg btn-success addItemButton">Add</button>
    <a href="#/"><button class="btn-lg btn-danger cancelButton">Cancel</button></a>
</form>

This is my app.module.js这是我的 app.module.js

var app = angular.module('ToDoListApp', ['ngRoute']);

app.config(function ($routeProvider, $locationProvider) {

    $locationProvider.hashPrefix('');

    $routeProvider
        .when("/", {
            templateUrl: "app/home/homePage.html",
        })
        .when("/newItem", {
            templateUrl: "app/newItem/newitem.html",
        })
        .otherwise({
            redirectTo: '/'
        });
});

controller for home page controller 主页

app.controller('homePageCtrl', function ($scope) {

});

controller for new item page controller 新品页面

app.controller('newItemCtrl', function ($scope) {

    $scope.toDoList = [{
        project: null,
        task: null,
        done: false
    }];

    $scope.addNewToDoTask = function () {
        $scope.toDoList.push({
            project: $scope.projectInput,
            task: $scope.taskInput,
            done: false
        });
        $scope.projectInput = "";
        $scope.taskInput = "";
    };
});

service to persist data across views跨视图持久化数据的服务

app.service('newToDoTaskService', function () {

});

So essentially when the form is submitted on the new item page, it will push a new object into the toDoList array, with the corresponding input values.所以基本上当在新项目页面上提交表单时,它会将新的 object 推入 toDoList 数组,以及相应的输入值。

What I then want to do, is use the project and task values from that object, to populate the H4 and p tags on the home page.然后我想做的是使用 object 中的项目和任务值来填充主页上的 H4 和 p 标签。

I know a good way of achieving this is to use a service, I'm just struggling to understand the concept even after reading the documentation.我知道实现这一目标的一个好方法是使用服务,即使在阅读文档后我也很难理解这个概念。

If someone could explain how I achieve this and breakdown the process, I would really appreciate it.如果有人能解释我是如何实现这一目标并分解流程的,我将不胜感激。

It's also worth mentioning clicking the add or cancel buttons on the new item page, will navigate you back to the home page.还值得一提的是,单击新项目页面上的添加或取消按钮,将带您返回主页。

Thanks谢谢

example service to persist data across views跨视图持久化数据的示例服务

app.service('taskService', function () {
    var taskArr = [];
    this.getTasks = function() {
        return taskArr;
    };
    this.addTask = function(task) {
        taskArr.push(task)
    };
});

To use, inject into the controller:要使用,请注入 controller:

controller for new item page controller 新品页面

app.controller('newItemCtrl', function ($scope, taskService) {

    $scope.addNewToDoTask = function () {
        taskService.addTask({
            project: $scope.projectInput,
            task: $scope.taskInput,
            done: false
        });
        $scope.projectInput = "";
        $scope.taskInput = "";
    };
});

controller for home page controller 主页

app.controller('homePageCtrl', function ($scope, taskService) {

    $scope.toDoList = taskService.getTasks();

});

For more information, see有关详细信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM