Angular 4 http服务调用不返回数据

时间:2017-08-15 17:09:29

标签: angular angular2-services

我有一个network.service.ts,代码在下面 -

namespace HelloWorld.ViewModel
{

    public class CustViewModel : INotifyPropertyChanged
    {

        private custmodel _custmodel;


        public custmodel custmodel
        {
            get { return _custmodel; }
            set
            {
                _custmodel = value;
                NotifyPropertyChanged();
            }
        }

        private string _message;
        public string message
        {
            get { return _message; }
            set
            {
                _message = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<string> _list;
        public ObservableCollection<string> Items
        {
            get
            {
        return _list;
            }
            set
            {
                _list = value;
                NotifyPropertyChanged();
            }
        }

        public Command SaveCommand
        {
            get
            {
                return new Command(() =>
                {
                    message = "Your task : " + custmodel.name + ", " + custmodel.surname + " was successfully saved!";
                });
            }
        }

        private string _bar;

        public string Bar
        {
            get { return _bar; }
            set { _bar = value;
        }
    }

    public Command SearchCommand
{
    get
    {
            return new Command(() =>
            {
                string keyword = _bar;
                IEnumerable<String> searchresult = _list.Where(name => name.Contains(keyword));
                _list = new ObservableCollection<string>(searchresult);
                NotifyPropertyChanged();
            }
                );


    }
}

public CustViewModel()
{

    custmodel = new custmodel
    {
        name = "Aasish",
        surname = "Gurung",
        email = "iamaaceez@yahoo.com"
    };
    _list = new ObservableCollection<string>();
    _list.Add("Saurab");
    _list.Add("Basanta");
    _list.Add("Abhishek");
    _list.Add("Surace");
    _list.Add("Amir");

    }

    public event PropertyChangedEventHandler PropertyChanged;


    //public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

我的peer-list.component.ts是组件文件。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="HelloWorld.Styling"
         BackgroundColor="AntiqueWhite" Title="Hello"
         xmlns:converters="clr-namespace:HelloWorld.Converters; assembly=HelloWorld">

<StackLayout>
    <SearchBar x:Name="MainSearchBar" Text="{Binding Bar}"
               SearchCommand="{Binding SearchCommand}"/>
    <ListView ItemsSource="{Binding Items}"/>
</StackLayout>

调用ngOnInit方法时,this.peers始终设置为undefined。数据从服务器返回,但回调无法将数据设置为this.peers变量。有人可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

USER

应该是

location

调用.then((response) => response.json().data as Peer[]) 将直接返回有效负载/数据。您可能希望执行.then((response) => response.json() as Peer[]) 的唯一原因是,如果您的json结果中存在名为json()的实际属性/字段。