0

I have a directive, form where the rest of the html is given. The directive is given below

THis is the html for directive

  <div test
  input="{{guage.input}}"
  >
  </div>

angular.module('test', [])

        .directive('test', function () {
            "use strict";

            return {

                restrict: 'A',

                scope: {
                    input: '='
                },

                templateUrl: 'gauge/gauge.tpl.html',

                replace: true
    });

The below is the html loaded after the directive compilation.

<div ng-controller="Testing">
<div>
    <div id="{{guageid}}" class="gauge ng-isolate-scope" ng-model="gauge.input" data-service="/api/getDataResult/core-mon-status" guageid="fleet_online" description="Fraction of fleet online" unit="%" test="{{gauge.test}}" input="{{gauge.input}}" gauge="">
    </div>
</div>
</div>

Now I have a parent controller above this dom element name is Testing.

From my controller if I change the {{guage.input}} its not displaying.

This is my controller

app.controller('Testing',['$scope','newdataLoad','$timeout',function($scope, newdataLoad, $timeout){
        $scope.gauge = {};
$scope.gauge.input = 0;
});

What is the problem with my scope here.

1
  • excuse my question but what does '=?' in the scope definition means? Commented Jun 12, 2014 at 14:28

2 Answers 2

1

As your scope defines the input with = you dont need the expression brackets.

<div test input="guage.input">

Using expression brackets will break the 2-way binding.


Optimization:

You can completely move you controller into the directive and still make use of the dependency injection

 "use strict"; 
    angular.module('test', []).directive('test', function () {   
       return {    
            restrict: 'A',    
            scope: {
              input: '='
            },    
            templateUrl: 'gauge/gauge.tpl.html',    
            replace: true,
            controller: function($scope, newdataLoad, $timeout){
               $scope.gauge = {};
               $scope.gauge.input = 0;
            }
       }
    });

The template code then :

<div>
    <div id="{{guageid}}" class="gauge ng-isolate-scope" ng-model="gauge.input" data-service="/api/getDataResult/core-mon-status" guageid="fleet_online" description="Fraction of fleet online" unit="%" test="{{gauge.test}}" input="{{gauge.input}}" gauge="">
    </div>
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

This may not be the only problem, but it certainly is one.
Hello, I got it working by making the changes. But This directive is there on multiple elements. And $watch triggers even if we change specific element.
$watch on what, which specific element?
I have multiple divs with same directive, so when specific change occurs in one place all the other elements are also updated. Say I change the guage value of 1 all other places also the guage value changes.
sure, because all directives are bound to variable gauge.input. if you have multiple different gauges, you need an other data structure or something
|
0

Remove curlies:

<div test input="guage.input">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.