Javascript ng更改不会更新视图AngularJS
我正在研究AngularJS,并尝试使用加载产品Javascript ng更改不会更新视图AngularJS,javascript,angularjs,asp.net-mvc-3,Javascript,Angularjs,Asp.net Mvc 3,我正在研究AngularJS,并尝试使用加载产品 $scope.update=function(){ $scope.loading = true; $scope.brandString=$scope.brands.join(':'); UpdateService.getProducts($scope).then(function (data) { $scope.loading = false; $scope.products = data.Products;}, func
$scope.update=function(){
$scope.loading = true;
$scope.brandString=$scope.brands.join(':');
UpdateService.getProducts($scope).then(function (data) { $scope.loading = false; $scope.products = data.Products;}, function () {
});
};
$scope.changepage=function(pagenum){
$scope.pagenumber=pagenum;
$scope.update();
};
$scope.changeorderby=function(orderby){
$scope.orderby=orderby;
$scope.pagenumber=1;
$scope.update();
};
$scope.updatebrands=function(id){
var index = $scope.brands.indexOf(id);
// Add if checked
if ($('#'+id).is(':checked')) {
if (index === -1) $scope.brands.push(id);
}
// Remove if unchecked
else {
if (index !== -1) $scope.brands.splice(index, 1);
}
$scope.update();
};
我的第二个和第三个功能,即changepage和changeorderby正在正常工作,并正在更新视图。在复选框更改时调用的第四个函数也会从服务器返回正确的数据,但不会更新我的视图
代码fo复选框
<div class="listbox">
<ul>
<div ng-class="{active : item.Checked, inactive:!item.Checked}" ng-repeat="item in brandDetails" style="padding:1px 0">
<div ng-class="{divchecked : item.Checked, divunchecked:!item.Checked}">
<input type="checkbox" id="{{item.Id}}" ng-change="updatebrands(item.Id)" ng-model="item.Checked" name="{{item.Name}}" value="{{item.Id}}" style="opacity:0" class="brandName ng-pristine ng-valid" /></input>
</div>
<span style="margin-left: 8px; font-size: 11px;top: -1px;position: relative;">{{item.Name}}</span>
</div>
</ul>
</div>
这里我缺少什么?如果UpdateService.getProducts不使用原始angular$http服务,那么angular将不知道发生了什么,所以您需要调用$scope。$apply
$scope.update=函数{
$scope.loading=true;
$scope.brandString=$scope.brands.join':';
UpdateService.getProducts$scope.thenfunction数据{
$scope.loading=false;
$scope.products=data.products;
//仅当UpdateService.getProducts不是纯$http或$q服务时
$scope.$apply;
},功能{
//错误
};
};
$scope.changepage=functionpagenum{
$scope.pagenumber=pagenum;
$scope.update;
};
$scope.changeorderby=functionorderby{
$scope.orderby=orderby;
$scope.pagenumber=1;
$scope.update;
};
$scope.updatebrands=functionitem{
var指数=$scope.brands.indexOfitem.id;
//如果选中,则添加
如果项目已选中{
如果索引==-1$scope.brands.pushitem.id;
}
//如果未选中,请删除
否则{
如果索引!=-1$scope.brands.index,1;
}
$scope.update;
};
{{item.Name}
除了您最初的问题,您的HTML标记在几个地方都是错误的,例如div不允许是ul的直接子元素或是一个自动关闭元素,它没有结束标记。内联样式也有错误,跨距需要将显示属性设置为块或内联块,以便相应地呈现边距。也不要使用value={{item.Id}}ng模型注意这一点,Id/类也不应以其外观上的数字开头Id={{item.Id}