使用此关键字和controlsAs

时间:2017-06-23 09:44:25

标签: javascript angularjs

我已经在控制器中使用$scope对象转换为vm = this。我知道您必须在controllerAsview中说明state

我有两个表使用ng-repeat,第一个表工作正常,所有数据都填充,但第二个由于某种原因不起作用。我有一个游戏,用户点击的项目记录在控制台中,但第二个表格没有填充数据。与$scope一起正常工作。

知道我哪里错了吗?

var app = angular.module('myApp', ['ngRoute', 'ui.router']);

app.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', '$routeProvider',  function($stateProvider, $locationProvider, $urlRouterProvider, $routeProvider) {

    $urlRouterProvider
        .when('/', '/patents/')
        .otherwise('/patents/');

    $stateProvider
        .state("patents", {
            url: "/patents",
            templateUrl: "templates/patents/list/list-patents.htm",
            controller: "patentCtrl",
            controllerAs: "patents"
        })
        .state("patents.item", {
            url: "/:id",
            templateUrl: "templates/patents/list/patent-item.htm",
            controller: "patentCtrl",
            controllerAs: "item"
        })
}]);

app.factory('patentsService', function($http, $q) {

    var factory = {};

        var REST_SERVICE_URI = 'http://localhost:8080/Sprint002b/restpatent/';

        factory.fetchAllPatents = function() {

            var deferred = $q.defer();
             $http.get(REST_SERVICE_URI)
                .then(
                function (response) {
                    deferred.resolve(response.data);
                },
                function(errResponse){
                    console.error('Error while fetching Users');
                    deferred.reject(errResponse);
                }
            );

            return deferred.promise;
        }

        factory.select = function(item) { 
            selectedItem = item;
            return selectedItem;
        }        

    return factory;

});

app.controller('patentCtrl', ['patentsService', function(patentsService) {

    var vm = this;

    vm.patent={id:null, patentApplicationNumber:'', clientRef: '', renewalStatus: '', currentRenewalCost: '', costBandEndDate:'', renewalCostNextStage:'', renewalDueDate:''};
    vm.patents=[];

    vm.select = function(item) {
       vm.patentItem = patentsService.select(item); //THIS SHOULD POPULATE TABLE
       console.log(vm.patentItem)
    }

    vm.fetchAllPatents = function(){

        patentsService.fetchAllPatents()
            .then(
            function(d) {
                vm.patents = d;
            },
            function(errResponse){
                console.error('Error while fetching Users');
            }
        );
    }

    vm.fetchAllPatents();

}]);

工作表

<tbody>
    <tr ng-repeat="x in patents.patents">
        <td ng-click="patents.select(x)"><a ui-sref="patents.item({id: x.id})">{{x.applicationNumber}}</a></td>
        <td ng-bind="x.clientRef"></td>
        <td ng-bind="x.currentRenewalCost">$</td>
        <td ng-bind="x.costBandEndDate"></td>
        <td ng-bind="x.renewalCostNextStage"></td>
        <td ng-bind="x.renewalDueDate"></td>
    </tr>
</tbody>

破桌

<tbody>
    <tr ng-repeat="x in item.patentItem">
        <td>{{x.applicationNumber}}</td>
        <td>{{x.clientRef}}</td>
        <td>{{x.renewalStatus}}</td>
        <td>{{x.costBandEndDate}}</td>
        <td>{{x.renewalCostNextStage}}</td>
        <td>{{x.renewalDueDate}}</td>
      </tr>
</tbody>

1 个答案:

答案 0 :(得分:0)

然后你应该为scapy状态创建一个新的独立控制器。在加载的地方,您只需从patents.item检索当前选定的项目。

patentsService