$scope 在函数内部不起作用
$scope not working inside a function
我有以下代码。我在 html 上使用 ng-show 并尝试在 angular 函数上操作它,但它没有用。我不确定在函数内更改 $scope 值的正确方法。
在 Angular,
app.controller("ControlController", function($scope, $http, $location) {
$scope.badRequest = false;
$scope.login = function() {
$http.post('/login', {
email: $scope.email,
password: $scope.password
}).then function(res) {
$location.path('/');
},function(err) {
$scope.badRequest = true;
console.log($scope.badRequest); // this will print true.
//But this does not change the $scope.badRequest on DOM.
});
};
});
在翡翠上,我有
.incorrect-container(ng-show='badRequest')
有什么帮助吗?
不知道是不是你抄错了,但是“.then”是一个函数调用。应该是
$scope.login = function() {
$http.post('/login', {
email: $scope.email,
password: $scope.password
})
.then(function(data) {
// do stuff in here with successfully returned data
})
.catch(function(err) {
$scope.badRequest = true;
});
};
按照你的方式,.then
甚至都没有被调用。另外,当我按原样 运行 时,出现语法错误。你没收到吗?
我有以下代码。我在 html 上使用 ng-show 并尝试在 angular 函数上操作它,但它没有用。我不确定在函数内更改 $scope 值的正确方法。
在 Angular,
app.controller("ControlController", function($scope, $http, $location) {
$scope.badRequest = false;
$scope.login = function() {
$http.post('/login', {
email: $scope.email,
password: $scope.password
}).then function(res) {
$location.path('/');
},function(err) {
$scope.badRequest = true;
console.log($scope.badRequest); // this will print true.
//But this does not change the $scope.badRequest on DOM.
});
};
});
在翡翠上,我有
.incorrect-container(ng-show='badRequest')
有什么帮助吗?
不知道是不是你抄错了,但是“.then”是一个函数调用。应该是
$scope.login = function() {
$http.post('/login', {
email: $scope.email,
password: $scope.password
})
.then(function(data) {
// do stuff in here with successfully returned data
})
.catch(function(err) {
$scope.badRequest = true;
});
};
按照你的方式,.then
甚至都没有被调用。另外,当我按原样 运行 时,出现语法错误。你没收到吗?