Angular:无法从范围到达方法

时间:2016-03-14 14:58:20

标签: javascript angularjs ionic-framework

我正在尝试使用我的$ scope控制器中的方法,从Factory导入并在基于Ionic的HTML中的 onlick 方法上执行它。

我仍然得到 undefined ,我不知道为什么,因为来自$ scope的任何其他变量都可用(如userid)。此范围层次结构是否存在问我读了很多文章,但我仍然坚持。感谢。

控制器:

var mod = angular.module('Baybee.controller.nameList', []);

mod.controller('NameListCtrl', function ($scope, AllNamesList, UidGenerator, $localstorage) { 
//importing list of names from Factory and unique user-id
$scope.namesFromService = AllNamesList.getList('AllNamesList');
$scope.userid = UidGenerator;
// test functions which I wasn't able to execute on 'onclick' method. scope.saveName should be that I wanted to use originaly
$scope.saveName = AllNamesList.transferName;
$scope.testb = function () {
  console.log('working!');
  };
});    

厂:

var mod = angular.module('Baybee.factory.allNamesList',['ionic'])

.factory('AllNamesList', function($localstorage) {
// 3 more list available as variables, here is just one
var AllNamesList = {

names: [
  {
    "name": "Jakub",
    "set": [
      "CzechCalendar",
      "Top10Cz2015",
      "Top50Cz2010"
    ],
    "properties": {
      "sex": "male",
      "origin": "Hebrew",
      "description": "Comes from Hebrew, יַעֲקֹב (Yaʿqob, Yaʿaqov, Yaʿăqōḇ)"
    },
    "popularity": [
      {
        "Cz": {
          "2011": "10",
          "2012": "7",
          "2013": "6",
          "2014": "6",
          "2015": "7"
        }
      }
    ]
  }
// more names here
]
};


return {
// returns right list with names based on string argument
getList: function(a) {
  switch (a) {
    case "AllNamesList":
      return AllNamesList;
      break;
    case "loveNameList":
      return loveNameList;
      break;
    case "maybeNameList":
      return maybeNameList;
      break;
    case "noGoNameList":
      return noGoNameList;
      break;
    default:
      console.log("Sorry, can't find list with names");
  }
},
// I would like to use this function on onlick method to transfer object with name properties to the right list (
transferName: function(name, listFrom, listTo){
  // saving both list names into local memory
  for (i = 0; i < listFrom.length; i++) {
      if (name = listFrom[i]) {
        listTo.push(listFrom[i]);
          console.log(listFrom);
        listFrom.splice(i, 1);
          console.log(listTo);
      }
    else {
        console.log('Cannot find: ' + name + ' in ' + listFrom);
      }
   }

  }

 }

});

HTML正文:

<body ng-app="baybee">

<ion-pane ng-controller="NameListCtrl">
    <ion-header-bar class="bar-energized" >
        <h1 class="title" >Baybee</h1 >
    </ion-header-bar >
    <ion-content >

        <ion-list >
      <div ng-repeat="names in namesFromService track by $index">
            <ion-item ng-repeat="(key, value) in names"
                on-swipe-right=""
                on-swipe-left=""
          //I would like to execute any function from scope here, but Im getting undefined
                //onclick="testb()"
                //onclick="saveName(name, 'list1', 'list2')"

        <h2>{{value.name}}</h2>
        <p>{{value.properties.origin}}</p>
      </ion-item>
      </div>
        </ion-list >

    </ion-content >
</ion-pane >
</body >

1 个答案:

答案 0 :(得分:1)

HTML部分有错误,我使用onclick方法,应该是ng-click。

ng-click="saveName(name, 'list1', 'list2')"

由于