Angularjs Angular Js无法将数据推入控制器阵列
HTML:- }))Angularjs Angular Js无法将数据推入控制器阵列,angularjs,Angularjs,HTML:- })) })) 您的主要问题是$scope.newState在$scope.newStateState函数中不可用。在$scope函数中操纵像$scope这样的对象是不好的做法。事实上,您正在创建多个与您注入的$scope不同的对象;否则,两个不同国家/地区的输入框应匹配 在下面工作 JS: HTML: {{name}} {{country.name}
})) 您的主要问题是$scope.newState在$scope.newStateState函数中不可用。在$scope函数中操纵像$scope这样的对象是不好的做法。事实上,您正在创建多个与您注入的$scope不同的对象;否则,两个不同国家/地区的输入框应匹配 在下面工作 JS: HTML:
{{name}}
-
{{country.name}
-
{{data.newState}
当您处理诸如ng repeat、ng if或ng include之类的事情时,它们都会创建一个新的作用域。这意味着,当您尝试绑定到该作用域根级别上的基元时,它将在您更新该值时得到解引用。因此,需要将基本体放在对象内部。John Papa的风格指南建议在您的范围内创建一个“vm”属性。vm代表视图模型
下面是一个如何使用它的JSF简介
你能解释一下你的错误吗?看起来还可以。一支密码笔或一支扑克牌会很有帮助的。@Mike是对的。在将来,制作一个plunkr并发布它,让其他人更容易给出答案。很好的描述。你要在ng conf主持吗?你去年很搞笑#endofconversationinstackoverflowcommentWow!谢谢你,克里斯。这是我的一天,这一天才刚刚开始。
<div ng-controller="countryController">
{{name}}
<ul>
<li ng-repeat="country in countries">
{{ country.name }}
<ul ng-show="country.states.length > 0">
<li ng-repeat="state in country.states">
{{ state.name }}
</li>
</ul>
<input type="text" ng-model="newState">
<a href ng-click="addStateState(country)">Add </a>
{{newState}}
</li>
</ul>
</div>
myApp.factory('countryService', function($http){
var baseUrl = 'services/'
return {
getCountries : function(){
return $http.get(baseUrl + 'getcountries.php');
}
};
myApp.controller('countryController', function($scope, countryService){
countryService.getCountries().success(function(data){
$scope.countries = data;
});
$scope.addStateState = function(country){
country.states.push({'name' : $scope.newState});
$scope.newState = "";
};
angular.module('myApp',[])
.controller('countryController', function($scope){
$scope.countries = [
{ name: 'USA', states:[
{name: 'Virginia'},
{name: 'Utah'}
]
},
{ name: 'Brazil', states:[
{name: 'Pernabuco'}
]
}
];
$scope.addStateState = function(country, data){
country.states.push({'name' : data.newState});
data.newState = "";
};
});
<div ng-controller="countryController">
{{name}}
<ul>
<li ng-repeat="country in countries">
{{ country.name }}
<ul ng-show="country.states.length > 0">
<li ng-repeat="state in country.states">
{{ state.name }}
</li>
</ul>
<input type="text" ng-model="data.newState" />
<a href="" ng-click="addStateState(country, data)">Add </a>
{{data.newState}}
</li>
</ul>
</div>
vm.newState