我试图用 ember js 显示下拉列表。使用 Ajax GET 方法。但它无法返回任何值。
我的要求只有 Ajax
这是我的代码:
App.IndexController = Ember.ArrayController.extend({
model1: function () {
var Category1 ;
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
alert(data);
Category1 = data;
}
});
return Category1;
}.property(),
});
注意 在我的 Controller 中,警报显示正常。
警报 Json 是
[{"CountryID":1,"CountryName":"United States"}]
在我的 html 页面中无法获取该值。
这里是js代码
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" id="index">
<div>
{{view "select" id="AddProfile_SelectCountry" content=model1 prompt="Select Country" optionValuePath="content.CountryName" optionLabelPath="content.CountryName" valueBinding=default }}
</div>
</script>
<script src="js/libs/jquery-1.10.2.js"></script>
<script src="js/libs/handlebars-v1.3.0.js"></script>
<script src="js/libs/ember-1.8.1.js"></script>
<script src="js/appnew.js"></script>
<!-- to activate the test runner, add the "?test" query string parameter -->
最佳答案
当您执行 Category1 = data;
时,您将变量标签 Category1
设置为数据的值。您要做的是更改该标签引用的对象。
更好的做法是使 Category1
成为一个 Ember 对象,并通过数据设置其属性。像这样:
model1: function () {
var Category1 = Ember.Object.create();
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
alert(data)
Category1.setProperties(data);
}
});
return Category1;
}.property(),
如果您的数据以数组形式传入,但您只想要第一个元素(如上所示),您可以改为执行 Category1.setProperties(data[0]);
关于javascript - Ajax Get 方法不使用 Ember Js 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422408/