0

リソースを使用してAPIRestを使用して作成およびリストするコードがあります。テンプレートからコントローラー、およびコントローラーからサービスにIDを渡す必要があります。

これは私のコードです:

Template.html

<div class="industrialists" ng-app="cliConsApp">
    <ul class="table" ng-controller="industrialistCtrl" ng-init="init('{{ constructionPrivateInformationId }}')">
        <form name="myForm">
            <input type="text" id="userName" ng-model="industrialist.user" placeholder="User name"/>
            <input type="text" id="jobName" ng-model="industrialist.job" placeholder="Job name"/>
            <a ng-click="createNewUser()" class="btn btn-small btn-primary">create new user</a>
        </form>

        <li ng-repeat="industrial in industrialists">
            [[industrial.job.name]]
        </li>
    </ul>
</div>

app.js

var cliConsApp = angular.module('cliConsApp',['uTrans', 'cliConsApp.controllers', 'cliConsApp.services' ]).
    config(function($interpolateProvider){
        $interpolateProvider.startSymbol('[[').endSymbol(']]');
    }
);;

services.js

var services = angular.module('cliConsApp.services', ['ngResource']);

services.factory('IndustrialistsFactory', function ($resource) {
    return $resource(
        '/app_dev.php/api/v1/constructionprivateinformations/:id/industrialists',
        {id: '@id'},
        {
            query: { method: 'GET', isArray: true },
            create: { method: 'POST'}
        }
    )
});

controllers.js

var app = angular.module('cliConsApp.controllers', []);

app.controller('industrialistCtrl', ['$scope', 'IndustrialistsFactory',

    function ($scope, IndustrialistsFactory) {

        $scope.init = function (id) {

            $scope.id=id;
            $scope.industrialists= IndustrialistsFactory.query({},{id: $scope.id});

            $scope.createNewUser = function (id) {
                IndustrialistsFactory.create($scope.industrialist, {id: $scope.id});
                $scope.industrialists = IndustrialistsFactory.query({id: $scope.id});

            }
        }
}]);

サービスがIDを受信せず、URLが正しくないため、CreateNewUserに問題があります。

どうすればできますか?

4

1 に答える 1