单个控制器用于多个html段和来自ajax请求angularjs的数据

时间:2014-03-13 03:01:57

标签: angularjs angularjs-service angularjs-factory

我正在尝试使用相同的json数据显示我的html页面的两个部分,我不想将它们包装在同一个控制器中,因为它位于不同的区域。我已经通过在“角度服务”中使用本地json数据成功实现了这个概念,请参阅演示

<div ng-app="testApp">
<div ng-controller="nameCtrl">
    <a href="#" ng-click="addName();">Add New</a>
    <a href="#" ng-click="removeName();">Remove First</a>
    <ul id="first" class="navigation">
        <li ng-repeat="myname in mynames">{{myname.name}}</li>
    </ul>
</div>
<div>
    Lot of things in between
</div>

<ul id="second" class="popup" ng-controller="nameCtrl">
       <li ng-repeat="myname in mynames">{{myname.name}}</li>        
</ul>

JS

    var testApp = angular.module('testApp', []);


testApp.service('nameService', function($http) {
    var me = this;

    me.mynames = [
        {
            "name": "Funny1"
        },
        {
            "name": "Funny2"
        },
        {
            "name": "Funny3"
        },
        {
            "name": "Funny4"
        }
    ];

    //How to do
    /*this.getNavTools = function(){
        return $http.get('http://localhost/data/name.json').then(function(result) {
            me.mynames    = result.mynames;
            return result.data;
        });
    };*/



    this.addName = function() {
        me.mynames.push({
            "name": "New Name"
        });
    };


    this.removeName = function() {
            me.mynames.pop();
    };

});

testApp.controller('nameCtrl', function ($scope, nameService) {

    $scope.mynames        =   nameService.mynames;


    $scope.$watch(
        function(){ return nameService },

        function(newVal) {
            $scope.mynames          =  newVal.mynames;
        }
    )

    $scope.addName = function() {
        nameService.addName();
    }



    $scope.removeName = function() {
        nameService.removeName();
    }

});

jsfiddle

我想做的下一件事是向json文件发出http请求并使用数据加载我的两个部分,如果我添加或删除它应该反映在这两个区域。

任何指针或exisisitng演示都会有所帮助。

由于

2 个答案:

答案 0 :(得分:1)

只有一个ngRepeat正在更新的原因是因为它们绑定到两个不同的数组。

怎么会发生?这是因为您已拨打getNavTools()两次,并且在每次通话中,您已使用 new 数组替换了mynames!最终,addName()removeName()正在处理最后分配的mynames数组,因此您会看到问题。

我有你的修复:

testApp.service('nameService', function($http) {
    var me = this;
    me.mynames = []; // me.mynames should not be replaced by new result

    this.getNavTools = function(){
        return $http.post('/echo/json/', { data: data }).then(function(result) {
            var myname_json = JSON.parse(result.config.data.data.json);
            angular.copy(myname_json, me.mynames); // update mynames, not replace it
            return me.mynames;
        });
    };

    this.addName = function() {
        me.mynames.push({
            "name": "New Name"
        });
    };


    this.removeName = function() {
        me.mynames.pop();
    };
});

testApp.controller('nameCtrl', function ($scope, nameService) {

    // $scope.mynames        =   nameService.mynames; // remove, not needed

    nameService.getNavTools().then(function() {
        $scope.mynames = nameService.mynames;
    });

    /* Remove, not needed
    $scope.$watch(
        function(){ return nameService },

        function(newVal) {
            $scope.mynames          =  newVal.mynames;
        }
    );
    */

    $scope.addName = function() {
        nameService.addName();
    };

    $scope.removeName = function() {
        nameService.removeName();
    };
});

http://jsfiddle.net/z6fEf/9/

答案 1 :(得分:0)

你可以做的是将数据放在父范围内(可能在$ rootScope中)它会触发两个视图,而你不需要在这里观看..

 $rootScope.mynames        =   nameService.mynames;

请参阅jsFiddle