I have a simple Angular app (just learning) that I am trying to use with Angular Material. To just get something up and running I want to use <md-chips>
to add an array of attributes to a product model. Here's a snippet from the HTML:
// EXTRACT FROM VIEW
<md-content class="md-padding" layout="column">
<form name="productForm">
<section class="md-padding" layout="column">
<md-input-container flex>
<label ng-show="productForm.category.$pristine">e.g. Umbrella</label>
<input name="category" ng-model="product.category" ng-minlength="3" ng-maxlength="80" required>
</md-input-container>
</section>
<section class="md-padding" layout="column">
<md-chips ng-model="product.attributes">
<input type="text" placeholder="e.g. Size">
</md-chips>
</section>
</form>
</md-content>
And here's a snippet from the JS. Everything is working - except I am expecting the $scope.productAttrsLen
variable to update within the $scope.$watch('product')
call. The ProductService.product model updates .... but the $scope.productAttrsLen
doesn't. However - when I also run a $scope.$watchCollection
on product.attributes
then $scope.productAttrsLen
does update. Can anyone explain why, and how I can accomplish this all within the $scope.$watch('product'...)
call?? I've commented the code below:
// EXTRACT FROM CONTROLLER
myApp.controller('newProductController', [
'$scope', '$log', 'ProductService',
function (
$scope, $log, ProductService) {
$scope.product = ProductService.product || {};
$scope.product.attributes = [];
function getProductAttsLen() {
return _.size($scope.product.attributes);
}
$scope.productAttrsLen = getProductAttsLen();
// when I only include the following code,
// the product object updates (including the attributes)
// but productAttrsLen does not update.
$scope.$watch('product', function (product) {
ProductService.product = product;
$scope.productAttrsLen = getProductAttsLen();
});
// However, when I ALSO include the following code, the productAttrsLen
// does get updated. Why?? Ideally, it would all get sorted in the
// $scope.$watch('product'...) function call.
$scope.$watchCollection('product.attributes', function (attributes) {
$scope.productAttrsLen = getProductAttsLen();
});
}])