gpt4 book ai didi

knockout.js - MVC 5 使用 spa 模板无法显示 View

转载 作者:行者123 更新时间:2023-12-01 06:29:01 25 4
gpt4 key购买 nike

我有一个问题,我的所有 View 都显示出来了。 (没有js错误)
无论如何,当我去产品或家庭时,两种 View 都会显示,我不明白为什么。不 ko with: 应该限制一种或另一种观点吗?
我错过了什么?

索引.cshtml

@section SPAViews {
@Html.Partial("_Home")
@Html.Partial("_Products")
}
@section Scripts{
@Scripts.Render("~/bundles/knockout")
@Scripts.Render("~/bundles/app")
}

_Home.cshtml
<!-- ko with: home -->
...
<!-- /ko -->

_Products.cshtml
<!-- ko with: products -->
...
<!-- /ko -->

products.viewmodel.js
function ProductsViewModel(app, dataModel) {
var self = this;

self.query = ko.observable();

Sammy(function () {
this.get('#products', function () {
$.ajax({
method: 'get',
url: app.dataModel.productsUrl,
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
self.query(data.query);

}
});
})
});

return self;
}

app.addViewModel({
name: "Products",
bindingMemberName: "products",
factory: ProductsViewModel
});

home.viewmodel.js
function HomeViewModel(app, dataModel) {
var self = this;

Sammy(function () {
this.get('#home', function () {
console.log('home');
});
this.get('/', function () { this.app.runRoute('get', '#home') });
});

return self;
}

app.addViewModel({
name: "Home",
bindingMemberName: "home",
factory: HomeViewModel
});

捆绑:
 bundles.Add(new ScriptBundle("~/bundles/app").Include(
"~/Scripts/sammy-{version}.js",
"~/Scripts/app/common.js",
"~/Scripts/app/app.datamodel.js",
"~/Scripts/app/app.viewmodel.js",
"~/Scripts/app/home.viewmodel.js",
"~/Scripts/app/products.viewmodel.js",
"~/Scripts/app/_run.js"));

最佳答案

这可能是因为对 with 的误解。捆绑。 From the docs :

The with binding creates a new binding context, so that descendant elements are bound in the context of a specified object.



所以这只是切换上下文。它仍然会显示这两个部分。

您可能正在关注 the if binding (和相应的 ifnot binding )记录如下:

The if binding causes a section of markup to appear in your document (and to have its data-bind attributes applied), only if a specified expression evaluates to true...



你应该使用这个 if绑定(bind)并将表达式更改为计算结果为 bool 值的内容。

关于knockout.js - MVC 5 使用 spa 模板无法显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24766663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com