I tried to change a $scope variable in angularjs ng-click function. But angularjsdoes not seem to be able to do that.
Below is my code on my appController:
// appController.js
$scope email = "[email protected]";
$scope.emailInfo = "[email protected]";
//emailOnClick() is a ng-click on DOM. It will be triggered when the user click a button, which is to save his email information
$scope.emailOnClick = function() {
$scope.email = $scope.emailInfo;
console.log($scope.email); //this will print "[email protected]".
};
console.log($scope.email); // this will print "[email protected]", but I
//want it to print "[email protected]" as I triggered the ng-click function to
//apply the change.
console.log($scope.emailInfo); //this will print "[email protected]".
What do I miss? Any thought?
$scope.emailInfoin your controller with a default value that is not'', something like[email protected]and place that binding somewhere on the page. Once you click, see if it changes it.$scope, and you received answers stating that you should, so changing your question to inject$scopewill make this question and associated answers very confusing to others in the future. aside from that, the first line of code,$scope emailisn't even right (perhaps the absence of the.is a typo in the edit).console.logmessages are triggered, you don't show which controller these$scopevariables are declared in, and you don't show the HTML that triggers this function. If thoseconsole.logmessages are truly just in the base of the controller function, then it makes sense that they output the value that is set at the beginning, since they will output LONG before the function gets invoked.