Javascript 角度:无法从范围到达方法
我试图使用从工厂导入的$scope控制器中的方法,并在基于Ionic的HTML中的onlick方法上执行它 我仍然没有定义,我不知道为什么,因为$scope中的任何其他变量都是可用的(比如userid)。这是范围层次结构问题吗?我读了很多文章,但我还是被卡住了。谢谢 控制器:Javascript 角度:无法从范围到达方法,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,我试图使用从工厂导入的$scope控制器中的方法,并在基于Ionic的HTML中的onlick方法上执行它 我仍然没有定义,我不知道为什么,因为$scope中的任何其他变量都是可用的(比如userid)。这是范围层次结构问题吗?我读了很多文章,但我还是被卡住了。谢谢 控制器: var mod = angular.module('Baybee.controller.nameList', []); mod.controller('NameListCtrl', function ($scope, A
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);
}
}
}
}
});
var mod=angular.module('Baybee.factory.allNamesList',['ionic']))
.factory('AllNamesList',函数($localstorage){
//还有3个变量列表,这里只有一个
var AllNamesList={
姓名:[
{
“姓名”:“Jakub”,
“设置”:[
“捷克日历”,
“Top10Cz2015”,
“2010年前50名”
],
“财产”:{
“性别”:“男性”,
“起源”:“希伯来语”,
“描述”:“来自希伯来语,יַעֲקֹב(雅ʿ库布,雅ʿ亚科夫,雅ʿăqōḇ)"
},
“人气”:[
{
“Cz”:{
"2011": "10",
"2012": "7",
"2013": "6",
"2014": "6",
"2015": "7"
}
}
]
}
//这里有更多的名字
]
};
返回{
//根据字符串参数返回具有名称的右列表
getList:函数(a){
开关(a){
案例“所有名称列表”:
返回所有名称列表;
打破
案例“爱情名单”:
返回爱情名单;
打破
案例“maybeNameList”:
返回maybeNameList;
打破
案例“noGoNameList”:
回归无糖主义者;
打破
违约:
log(“对不起,找不到包含名称的列表”);
}
},
//我想在onlick方法上使用此函数将具有名称属性的对象转移到右侧列表(
transferName:函数(名称、listFrom、listTo){
//将两个列表名保存到本地内存中
对于(i=0;i
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 >
黄蜂
HTML部分有一个错误,我使用onclick方法,当它应该是ng click时
ng-click="saveName(name, 'list1', 'list2')"
谢谢onclick?你确定没有ngClick吗?这可能是粘贴代码的错误,但以防万一:你是否将Baybee.factory.allNamesList
作为依赖项包含在你的Baybee.controller.nameslist
模块中?否则你可以执行$scope.allNamesList=allNamesList;
并在html中像ng click一样使用它=“AllNamesList.transfertName()”
或者您也可以将其放入函数中,如$scope.saveName=function($event){AllNamesList.transfertName($scope.name);}
你完全应该像@dfsq所说的那样使用ng click
。爱奥尼亚甚至有一个onClick指令吗?我还以为它在tap上呢。saveName需要包装在控制器内的一个函数中,例如:$scope.saveName=function(){AllNamesList.transferName();};