简体   繁体   English

如何在angularjs中推送数据

[英]How to push data in angularjs

there is an issue when I'm adding data to list with a function called addGroup.It is giving me this type-error: 当我使用名为addGroup的函数向列表添加数据时出现问题,这给了我这种类型错误:

   "TypeError: Cannot read property 'push' of undefined"
    at r.$scope.addGroup (main.js:7)
    at fn (eval at compile (angular.js:13365), <anonymous>:4:215)
    at e (angular.js:23613)
    at r.$eval (angular.js:16052)
    at r.$apply (angular.js:16152)
    at HTMLAnchorElement.<anonymous> (angular.js:23618)
    at HTMLAnchorElement.dispatch (jquery.min.js:3)
    at HTMLAnchorElement.q.handle (jquery.min.js:3)
(anonymous) @ angular.js:12520
(anonymous) @ angular.js:9292
$apply @ angular.js:16157
(anonymous) @ angular.js:23618
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3

html code: html代码:

<ul ng-repeat = "info in infos | filter:curInfo.name">
       <img src="{{info.image.link}}"/> {{info.name}}
        <li ng-repeat="group in info.groups | filter: curInfo" 
            ng-bind-html="group.name | highlight:curInfo.name">
           <a href="#">{{group.name}}</a>
        </li>
             <div class="add list">
                <a href="" ng-click="addGroup()">+Add group </a>
             </div>
    </ul>

js code: js代码:

app.controller('mainCtrl', function($scope , dataService){
        $scope.addGroup = function () {
            var group = {name: "This is a new group"};
            $scope.infos.push(group);
        };

json data json数据

[
   {
      "id":736,
      "name":"Systems",
      "image":{
         "link":"http://i.stack.imgur.com/8KA9j.jpg?s=32&g=1"
      },
      "groups":[
         {
            "id":2168,
            "name":"API",
            "url":"https://wwww.itschools.co.za/api/"
         },
         {
            "id":11955,
            "name":"Assets",
            "url":"https://wwww.itschools.co.za/assets/"
         },
         {
            "id":3179,
            "name":"Design",
            "url":"https://wwww.itschools.co.za/design/"
         },
         {
            "id":207,
            "name":"Development",
            "url":"https://wwww.itschools.co.za/development/"
         },
         {
            "id":70,
            "name":"Intranet",
            "url":"https://wwww.itschools.co.za/intranet/"
         }
      ],
      "url":"https://wwww.itschools.co.za/projects"
   },
   {
      "id":44315,
      "name":"User Agents",
      "image":{
         "link":"http://www.zerohedge.com/sites/default/files/pictures/picture5781.jpg"
      },
      "groups":[
         {
            "id":191599,
            "name":"Alchemy",
            "url":"https://wwww.itschools.co.za/tools/alchemy"
         },
         {
            "id":86822,
            "name":"Empathy",
            "url":"https://wwww.itschools.co.za/tools/empathy"
         },
         {
            "id":86297,
            "name":"Epiphany",
            "url":"https://wwww.itschools.co.za/tools/epiphany"
         },
         {
            "id":131837,
            "name":"Harmony",
            "url":"https://wwww.itschools.co.za/tools/hamony"
         },
         {
            "id":174338,
            "name":"Zagreb",
            "url":"https://wwww.itschools.co.za/tools/zagreb"
         }
      ],
      "url":"https://wwww.itschools.co.za/tools"
   }
]

I have used a navigation bar to show this data. 我已使用导航栏显示此数据。 the "groups" part is shown in “组”部分显示在

  • and now I want the addGroup function to add new list items but it is added in ul tag. 现在,我希望addGroup函数添加新的列表项,但是将其添加到ul标签中。

  • The main issue seems to be an uninitialized $scope.infos variable. 主要问题似乎是未初始化的$scope.infos变量。 I'm guessing this variable's contents are fetched via your dataService service, and that your intention is to push to the groups member of some element of $scope.infos . 我猜想此变量的内容是通过您的dataService服务获取的,您的意图是将其推送到$scope.infos某个元素的groups成员。

    To do that, your addGroups will need to know which groups to push into, so either info or info.groups will need to be passed in as a parameter (I went with the latter). 为此,您的addGroups将需要知道要推入的groups ,因此infoinfo.groups将需要作为参数传递(我同意后者)。

    Here's a pared down version of what I think you're trying to accomplish. 这是我认为您要实现的目标的精简版本。

     angular.module('app', []) .factory('dataService', ['$q', function($q) { //dummy service simulating actual dataService var infos = [ { "id":736, "name":"Systems", "image":{ "link":"http://i.stack.imgur.com/8KA9j.jpg?s=32&g=1" }, "groups":[ { "id":2168, "name":"API", "url":"https://wwww.itschools.co.za/api/" }, { "id":11955, "name":"Assets", "url":"https://wwww.itschools.co.za/assets/" }, { "id":3179, "name":"Design", "url":"https://wwww.itschools.co.za/design/" }, { "id":207, "name":"Development", "url":"https://wwww.itschools.co.za/development/" }, { "id":70, "name":"Intranet", "url":"https://wwww.itschools.co.za/intranet/" } ], "url":"https://wwww.itschools.co.za/projects" }, { "id":44315, "name":"User Agents", "image":{ "link":"http://www.zerohedge.com/sites/default/files/pictures/picture5781.jpg" }, "groups":[ { "id":191599, "name":"Alchemy", "url":"https://wwww.itschools.co.za/tools/alchemy" }, { "id":86822, "name":"Empathy", "url":"https://wwww.itschools.co.za/tools/empathy" }, { "id":86297, "name":"Epiphany", "url":"https://wwww.itschools.co.za/tools/epiphany" }, { "id":131837, "name":"Harmony", "url":"https://wwww.itschools.co.za/tools/hamony" }, { "id":174338, "name":"Zagreb", "url":"https://wwww.itschools.co.za/tools/zagreb" } ], "url":"https://wwww.itschools.co.za/tools" } ]; return { infos: function() { return $q.resolve(infos); } } }]) .controller('mainCtrl', ['$scope', 'dataService', function($scope, dataService) { dataService.infos().then(function(infos) { $scope.infos = infos; }); $scope.addGroup = function(groups) { var group = {name: "This is a new group"}; groups.push(group); }; }]) ; 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script> <div ng-app="app"> <div ng-controller="mainCtrl"> <!-- filter and highlight stuff removed for simplicity --> <ul ng-repeat = "info in infos"> <img src="{{info.image.link}}"/> {{info.name}} <li ng-repeat="group in info.groups"> <a ng-href="{{group.url}}">{{group.name}}</a> </li> <div class="add list"> <a href="" ng-click="addGroup(info.groups)">+Add group </a> </div> </ul> </div> </div> 

    The dataService is mocked up since it wasn't provided in the question, but it should be clear on how to transform that into something that could fetch the contents via eg the $http service. 由于未在问题中提供dataService ,因此已对其进行了模拟,但是应该清楚如何将其转换为可以通过$http服务获取内容的内容。

    One other change was to use ng-href="{{group.url}}" on your per-group <a> s; 另一个更改是在每个组<a>上使用ng-href="{{group.url}}" it wasn't mentioned but I assumed that's what the url member was for. 它没有被提及,但我认为这就是url成员的用途。

    Try this 尝试这个

    app.controller('mainCtrl', function($scope , dataService){
            $scope.addGroup = function () {
                $scope.infos = [];
                var group = {name: "This is a new group"};
                $scope.infos.push(group);
            };
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM