$scope 不更新,除非调用两次
$scope not updating unless called twice
请看下面我的代码。在我看来,我很难显示 $scope.brand 的价值。当我点击 获取用户品牌按钮 时,没有任何反应。视图中的 brand.name 没有更新,但是当我在单击 获取用户品牌按钮 后单击 测试品牌按钮 时,范围的值将显示。我不知道现在发生了什么。请帮忙。谢谢!
Angular
var app = angular.module('nwApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
var url = 'http://localhost:3000';
app.controller('brandsCtrl', ['$scope', '$http', function($scope, $http){
$scope.brands = {};
$scope.brand = {};
$http.get(url+'/brands/all')
.success(function(result){
$scope.brands = result;
});
$scope.getAssignUser = function(brand_id){
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.brand = result;
});
$("#brandModal").modal('toggle');
};
$scope.checkBrand = function(){
console.log($scope.brand);
}
}]);
查看
<a href="javascript:void(0)" class="btn btn-warning" title="Assign User for this Brand" ng-click='getAssignUser(brand._id)' >GET USER BRAND</a>
<a href="javascript:void(0)" class="btn btn-warning" ng-click='checkBrand()' > TEST BRAND </a>
<div><% brand.name %></div>
问题出在这里:
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.brand = result;
});
$.get
是 jquery,因此当它成功更新 $scope.brand 时,摘要循环将不会 运行 并反映该值。
您应该利用 angular 提供的 $http
服务来完成 ajax:
$http({
method: 'GET',
url: url+'/brands/'+brand_id
}).then(function successCallback(result) {
$scope.brand = result;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
其他解决方案是:
在您的 jquery ajax 中,您像这样更改 $apply
内的范围变量:
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.$apply(function (){$scope.brand = result;});
});
请看下面我的代码。在我看来,我很难显示 $scope.brand 的价值。当我点击 获取用户品牌按钮 时,没有任何反应。视图中的 brand.name 没有更新,但是当我在单击 获取用户品牌按钮 后单击 测试品牌按钮 时,范围的值将显示。我不知道现在发生了什么。请帮忙。谢谢!
Angular
var app = angular.module('nwApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
var url = 'http://localhost:3000';
app.controller('brandsCtrl', ['$scope', '$http', function($scope, $http){
$scope.brands = {};
$scope.brand = {};
$http.get(url+'/brands/all')
.success(function(result){
$scope.brands = result;
});
$scope.getAssignUser = function(brand_id){
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.brand = result;
});
$("#brandModal").modal('toggle');
};
$scope.checkBrand = function(){
console.log($scope.brand);
}
}]);
查看
<a href="javascript:void(0)" class="btn btn-warning" title="Assign User for this Brand" ng-click='getAssignUser(brand._id)' >GET USER BRAND</a>
<a href="javascript:void(0)" class="btn btn-warning" ng-click='checkBrand()' > TEST BRAND </a>
<div><% brand.name %></div>
问题出在这里:
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.brand = result;
});
$.get
是 jquery,因此当它成功更新 $scope.brand 时,摘要循环将不会 运行 并反映该值。
您应该利用 angular 提供的 $http
服务来完成 ajax:
$http({
method: 'GET',
url: url+'/brands/'+brand_id
}).then(function successCallback(result) {
$scope.brand = result;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
其他解决方案是:
在您的 jquery ajax 中,您像这样更改 $apply
内的范围变量:
$.get(url+'/brands/'+brand_id)
.success(function(result){
$scope.$apply(function (){$scope.brand = result;});
});