2

我需要编译一条指令,因为它需要一个从数据库返回的ID,但似乎无法得到该指令持久化的指令。隔离范围变量出现的编译指令undefined

$scope.orderInfo = { 
    orderId: 'lkdfj232jh' 
}; 
$scope.compile = function() { 
    var html = $compile('<apps orderId="orderInfo.orderId"></apps>')($scope); 
    $('#apps').append(html); 
}; 

return { 
    templateUrl: 'apps.html', 
    restrict: 'E', 
    scope: { 
    orderId: '=' 
    }, 
    controller: function($scope) { 
    console.log($scope.orderId); 
    } 
} 

orderId总是未定义在指令中。

Plunker

+0

属性名称应该是'订单id'不'orderId' – 2015-03-31 16:07:19

回答

2

属性名称应为order-id(连字符)不是orderId

$scope.compile = function() { 
    var html = $compile('<apps order-id="orderInfo.orderId"></apps>')($scope); 
    $('#apps').append(html); 
}; 

Working Plnkr