在POST上将REST值设置为$ scope.value为NULL但接受硬值

时间:2014-12-29 18:55:51

标签: angularjs angularjs-scope

我有一个简单的表单发布到我的REST后端。我在表单上总共有两个字段,但我想要填充三个值。

使用以下表格,我让用户输入articletitle和articlesummary

<div ng-controller="pfcPost">
<form ng-submit="createTask()">
    <input type="text" ng-model="newTask.articletitle"
           placeholder="add new task here">
    <input type="text" ng-model="newTask.articlesummary"
           placeholder="add summary">
    <input type="submit" value="create">
</form>
</div>

但是,我还想在我的数据库中为articlelink设置一个值作为articletitle(稍后,我会将其改编为没有空格的标题等)。

这是我的控制器:

    pfcControllers.controller('pfcPost', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {

    $scope.newTask = new pfcArticles(
        {
        articletitle: $scope.articletitle,
        articlesummary: $scope.articlesummary,
        articlelink: $scope.articletitle
        });
    $scope.createTask = function () {
        // call the service
        $scope.newTask.$save(function () {
            // when saved, reload the list and recreate a new task
        });
    }
}]);

目前,它不会将$ scope.articletitle的值发布到articlelink,但会将其显示为null

但是,如果我将其更改为硬值,如下所示,则可以正常工作。

        $scope.newTask = new pfcArticles(
        {
        articletitle: $scope.articletitle,
        articlesummary: $scope.articlesummary,
        articlelink: "hard value"
        });

这是后端工厂:

    var pfcServices = angular.module('pfcServices', ['ngResource'])

pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
    return $resource('https://myrestcall.net/articles/:articleID', { articleID: '@id' },
    {
        'update': { method:'PATCH'}
    }
    );
}]);

为什么不将范围项作为值的任何线索?其他值填充没有问题。

1 个答案:

答案 0 :(得分:0)

我通过以下方式工作:

输入:

<div ng-controller="pfcPost">
<form ng-submit="createTask()">
    <input type="text" ng-model="articletitle"
           placeholder="add new task here">
    <input type="text" ng-model="articlesummary"
           placeholder="add summary">
    <input type="submit" value="create">
</form>

</div>

控制器:

    pfcControllers.controller('pfcPost', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {

    var articleentry = new pfcArticles;

    $scope.createTask = function () {

        articleentry.articletitle = $scope.articletitle;
        articleentry.articlesummary = $scope.articlesummary;
        articleentry.articlelink = $scope.articletitle;

        articleentry.$save();
    }
}]);