0

jqueryを使用してドロップダウンを作成するためのKnockOutに関するコードも取得しました。私は KnockOut にまったく慣れていないので、それがどのように機能するかを理解できるようになりました。

ノックアウト関連のコードの意味を教えてください。ここに完全なコードがあります

<p>
    Your country:
    <asp:DropDownList ID="ddlCountries" runat="server" data-bind="options: countryModel.countries, optionsValue: 'CountryID', optionsText: 'CountryName', optionsCaption: 'Choose...'">
    </asp:DropDownList>
</p>
<input type="button" value="Add" data-bind="click: addCountry" />

<script type="text/javascript">
    function DropDownModel() {
        var self = this;
        self.countries = ko.observableArray();
        self.addCountry = function () {
            $.ajax({
                type: "POST",
                url: "DropDownSolution.aspx/GetCountries",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    self.countries(data.d);
                }
            });
        };
    }
    var countryModel = new DropDownModel()
    ko.applyBindings(countryModel);
</script>

    WebMethod]
    public static List<Country> GetCountries()
    {
        List<Country> countries = new List<Country>();
        countries.Add(new Country { CountryID = "1", CountryName = "India" });
        countries.Add(new Country { CountryID = "2", CountryName = "Singapore" });
        countries.Add(new Country { CountryID = "3", CountryName = "Malaysia" });
        return countries;
    }

私の懸念は、KnockOut 関連のコードを理解できないことです。

ko.applyBindings(countryModel);

コって何?

var self = this;
self.countries = ko.observableArray();
self = this means what....so self hold which reference?

self.countries = ko.observableArray();
what is ko and how ko comes?
what is observableArray() & what is does?

self.addCountry = function () { addCountry DropDownModel() 関数が呼び出されると自動的に発火するようです。どのように関数を自動的に呼び出すことができますか?

KnockOut がドロップダウンに入力する方法....ドロップダウンに入力する方法を理解する方法。

詳しく教えてください。ありがとう

4

1 に答える 1

0

コとは何ですか?

koグローバルノックアウトオブジェクトです。knockout.jsスクリプトを含めると追加されます。これは$jqueryの変数のようなものです。

self.addCountry = function(){addCountryは、DropDownModel()関数が呼び出されると自動的に起動するようです。関数を自動的に呼び出すにはどうすればよいですか?

この関数は自動的には呼び出されません。代わりに、次のように追加ボタンに接続されています。

<input type="button" value="Add" data-bind="click: addCountry" />

addCountryこれにより、メソッドがclickボタン入力のハンドラーにバインドされます。

KnockOutがドロップダウンにデータを入力する方法....ドロップダウンにデータを入力する方法を理解する方法。

ドロップダウンは、例の次のコードに示すように、バインディングを使用して入力optionsされます。

// Note that I removed some of the other attributes so that it is 
// easier to see the knockout only attributes.
<asp:DropDownList data-bind="options: countryModel.countries, optionsValue: 'CountryID', optionsText: 'CountryName', optionsCaption: 'Choose...'">
</asp:DropDownList>

optionsそれぞれの特定のバインディング値のドキュメントはここにあります

また、knockoutjs.comでノックアウトのすべてのドキュメントを見つけることができます。

于 2012-08-29T18:49:19.593 に答える