首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ng错误-用这个关键字和控制器替换$scope时重复

用ng错误-用这个关键字和控制器替换$scope时重复
EN

Stack Overflow用户
提问于 2017-06-23 09:44:25
回答 1查看 44关注 0票数 1

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

我有两个使用ng-repeat的表,第一个很好,所有的数据都被填充,但是第二个表由于某种原因没有被填充。我已经玩了一遍,用户单击的项被记录在控制台中,但是第二个表没有填充数据。和$scope合作得很好。

知道我哪里出问题了吗?

代码语言:javascript
复制
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();

}]);

工作台

代码语言:javascript
复制
<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>

破台

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2017-06-23 09:59:52

然后,您应该为patents.item状态创建一个新的单独控制器。在加载时,您将只从patentsService中检索当前选定的项。

代码语言:javascript
复制
//config
.state("patents.item", {
    url: "/:id",
    templateUrl: "templates/patents/list/patent-item.htm",
    controller: "patentItem", //<-- controller added here
    controllerAs: "item"
})
//patenItem new controller
.controller('patentItem', ['patentsService', function(patentsService) {
    var item = this;
    item.select = function(items) {
        vm.patentItem = patentsService.select(item);
        console.log(vm.patentItem)
    };
    function init(){
       item.select();
    }
    init();
}]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44718234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档