javascript - Can I use for ... in syntax for angular models? -
there $scope.model object in controller. properties fetched server on startup , bound input fields in view.
<input type="text" ng-model="model.name" placeholder="name"/> <input type="email" ng-model="model.email" placeholder="email"/> <!-- ... --> when want loop on properties of model came either server or form fields. example, need find properties changed send server.
$scope.save = function() { // filter changed properties var changed = {}; (i in $scope.model) { if (i in initial && model[i] == initial[i]) continue; changed[i] = model[i]; } // send them server // ... } however, don't know whether angularjs internally adds properties object. safe utilize syntax or loop on angularjs specific properties shouldn't see or manipulate?
angular prefix own stuff $ if want safe check key not start that.
if, example, you're using ng-repeat angular add together $$hashkey property.
and you'll never know if they'll start adding other properties in future, if they (most likely) follow convention prefixing it.
$scope.save = function() { var changed = {}; // filter changed properties (i in $scope.model) { // skip angular prefix if (i.length && i[0] == '$') continue; // skip if not changed if (i in initial && model[i] == initial[i]) continue; // add together result changed[i] = model[i]; } // send them server // ... } javascript angularjs for-in-loop
No comments:
Post a Comment