user1594252
user1594252

Reputation: 3

angularJS $watch do not work in directive when use controller to update data

I have a custom directive,and also have a controller to bind the data to directive.

I get the data from the server part and bind it to directive.However,I found the data of directive on page do not update when I change scope variable

Here is my code

directive:

angular.module('MyApp')
.directive('stats',function() {
    return {
    templateUrl:'scripts/directives/dashboard/stats/stats.html',
    restrict:'E',
    replace:true,
    scope: {
    'comments': '@',
    'number': '@',
    'name': '@',
    'colour': '@',
    'details':'@',
    'type':'@',
    'goto':'@'
    },
  link : function($scope,element,attr){
    $scope.$watch('number', function(oldValue,newValue) {
           console.log(attr);  
        }, true);
  }

}
});

directive template:

<div class="col-lg-3 col-md-6">
<div class="panel panel-{{colour}}">
    <div class="panel-heading">
        <div class="row">
            <div class="col-xs-3">
                <i class="fa fa-{{type}} fa-5x"></i>
            </div>
            <div class="col-xs-9 text-right">
                <div class="huge">{{number}}</div>
                <div>{{comments}}</div>
            </div>
        </div>
    </div>
    <a href="{{goto}}">
        <div class="panel-footer">
            <span class="pull-left">查看详情</span>
            <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
            <div class="clearfix"></div>
        </div>
    </a>
</div>

controller:

'use strict';
angular.module('MyApp',['ngResource'])

.controller('MainCtrl', function($scope,$state,MyService) {
    $scope.result = {};
    var names = MyService.get({classtype:'getNames',start:'',end:''},function(){
        $scope.pages = names.data;
        if (typeof($scope.pages[0]) === 'undefined'){
            $scope.selectedItem = 'loading...';
        }else{
            $scope.selectedItem = $scope.pages[0].name;
        }
        var res = MyService.get({classtype:'getLastRes',seriesName:$scope.selectedItem},function(){
            $scope.result = res;
        });

    });
    $scope.dropboxitemselected = function(item){
        $scope.selectedItem = item;
        var result = MyService.get({classtype:'getLastRes',seriesName:item},function(){
            $scope.result = result;
        });
        //$scope.result = {};
    };



});

HTML:

<div class="row" ng-controller="MainCtrl">

    <stats number="{{result.score}}" comments="score" colour="primary" type="heartbeat" goto="#/res/{{result._id}}"></stats>
    <stats number="{{result.totalSize}}" comments="size" colour="primary" type="file-code-o" goto="#/res/{{result._id}}"></stats>
    <stats number="{{result.count}}" comments="count" colour="red" type="file-text" goto="#/res/{{result._id}}"></stats>

</div>

there is a dropdown box on my page,I need refresh data in directive when I change item by function dropboxitemselected in controller,How can I do?

Upvotes: 0

Views: 493

Answers (1)

LooMan
LooMan

Reputation: 21

I think it is because of the scope bindings, you should use '=' for two way binding instead of '@'.

scope: {
    'number': '=',
    },

And in your HTML remove the brackets from number.

<stats number="result.score" comments="score" colour="primary" type="heartbeat" goto="#/res/{{result._id}}"></stats>

Upvotes: 2

Related Questions