Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive Isolate Scope 1.2.2

I'm working with Angular version 1.2.2 for the first time and trying to make a simple directive that uses isolate scope with '=' binding to pass in an object. I've done this a few times before so I'm wondering if maybe there was a change in 1.2.2 that changed this?

Here is my directive:

.directive('vendorSelector', function (VendorFactory) {
    return {
        restrict: 'E',
        replace: true,
        scope: { vendorId: '=' },
        template:   '<select ng-model="vendorId" ng-options="id for id in vendorIds">' +
                        '<option value="">-- choose vendor --</option>' +
                    '</select>',
        link: function (scope, element, attrs) {
            VendorFactory.getVendorIds().then(function(result) {
                scope.vendorIds = result;
            });
        }
    }
})

My HTML template using the directive is as follows:

<div class="padding">
    <vendor-selector vendorId="someValue"></vendor-selector>
    {{ someValue }}
</div>

And the backing controller:

.controller('AddProductController', function($scope, ProductFactory, AlertFactory) {
    $scope.vendorId = 0;
    $scope.someValue = undefined;
})

I've tried using both $scope.someValue and $scope.vendorId as the supplied object in the html template. In both cases the error I'm getting back is Expression 'undefined' used with directive 'vendorSelector' is non-assignable!. Am I missing something obvious that is preventing these values from being 2-way bound in the isolate scope?

like image 516
Jesse Carter Avatar asked Apr 30 '26 13:04

Jesse Carter


1 Answers

In your html:

<vendor-selector vendorId="someValue"></vendor-selector>

Change vendorId="someValue"

to vendor-id="someValue"

HTML attributes are case insensitive so to avoid confusion Angular converts all camel cased variables (vendorId) to snake case attributes (vendor-id).

So someValue wasn't bound to vendorId. Resulting in vendorId being undefined in the template. And thus your error.

like image 77
KayakDave Avatar answered May 03 '26 03:05

KayakDave