我可以在 JavaScript 中定义另一个变量的同时设置一个变量的值吗?
$scope.modal.data = {};
$scope.modal.submitDisabled = true
我有这两种说法。有没有办法可以将它们组合成一个?
我可以在 JavaScript 中定义另一个变量的同时设置一个变量的值吗?
$scope.modal.data = {};
$scope.modal.submitDisabled = true
我有这两种说法。有没有办法可以将它们组合成一个?
If those two fields are the only properties (NB: not "variables") of modal
, then sure, you can completely overwrite $scope.modal
in one operation:
$scope.modal = {data: {}, submitDisabled: true};
If you have other fields within modal
that you want to preserve, then no, don't bother - you'll be making your code harder to read.
That said, if you're using AngularJS ($scope
suggests you might be) you can also do:
angular.extend($scope, { data: {}, submitDisabled: true} );
which copies the new properties into $scope
without removing any other properties.