2016-03-05 101 views
0

我有對象控制器角指令表不走了

$scope._id = '12345'; 
$scope.messages = { 
    '12345': [ 
     { 
     author: 'Vasya', 
     message: 'Message from Vasya' 
     }, 
     { 
      author: 'Bob', 
      message: 'Message from Bob' 
     }] 
}; 

HTML標記:

<div message-list="{{_id}}")></div> 

我的指令:

var App = angular.module('app', []) 
.directive("messageList", function() { 
    return { 
     link: function(scope,element,attrs) {   
      scope.$watch('messages[attrs.messageList]', function(newVal,oldVal) { 
       if (newVal!==oldVal) 
       { 
        console.log("newVal",newVal); 
        console.log("oldVal",oldVal); 
       } 
      },true); 
     } 
    } 
}); 

,但看不,當我工作將新元素推入數組$ scope.messages ['12345']。

+0

如果我是正確的,你想觀看的範圍是「指令範圍」,並且不與控制器範圍相關,其中的對象「消息「是。 嘗試直接在控制器中觀看或使用Rootcope。 $ root351, – icchio

回答

0

這裏是一個plunkr.在查看plunker的同時打開控制檯查看更改。

您將需要一個$watchmessages對象:

scope.$watch('messages', function(newVal,oldVal) { 
    if (newVal!==oldVal) 
    { 
     console.log("newVal",newVal); 
     console.log("oldVal",oldVal); 
    } 
},true);