Angular指令属性不与控制器绑定

时间:2016-08-24 15:57:35

标签: angularjs

我有一个名为orders-list的指令,我需要将控制器内的对象currentOrder与被点击的循环内的对象绑定;

//controler
app.controller("receivedOrders", ['$scope', 'orders', 'Order', 'currentOrder', function ($scope, orders, Order, currentOrder) {
  $scope.currentOrder = null;
  $scope.orders = orders['content'];


//directive
app.directive("ordersList", function () {
  return {
    restrict: 'E',
    templateUrl: 'templates/orders_list.html',
    scope: {orders: '=', currentOrder: '='},
    controller: ['$scope','$attrs', function ($scope, $attrs) {
        $scope.selectOrder =function(order){
            $attrs.currentOrder=order;
        };
     //some other functions
    }]
}

//directive definition
<orders-list current-order="currentOrder" orders="orders"></orders-list>

//directive body
<md-list>
    <order ng-repeat="order in orders|filter:query|orderBy:activeSorting.directionSign+activeSorting.name"
           order="order" ng-click="selectOrder(order)"></order>
</md-list>

--------改变---------

现在好了,我将函数selectOrder传递给内部指令顺序,并从它的作用域调用selectOrder函数,但它仍然不起作用=(

app.directive('order', function () {
return {
    restrict: "E",
    templateUrl: '/templates/order.html',
    scope: {
        order: '=',
        orderClick:'&'
    },

<md-list-item order-click="orderClick({order:order})" class="md-3-line">

<order ng-repeat="order in orders|filter:query|orderBy:activeSorting.directionSign+activeSorting.name"
           order="order" order-click="selectOrder(order)"></order>

2 个答案:

答案 0 :(得分:1)

这是因为ng-repeat为orders属性中的每个元素创建了一个新范围。

  

ngRepeat指令从a中为每个项目实例化一次模板   采集。每个模板实例都有自己的范围,给定的范围   loop变量设置为当前集合项,并设置$ index   到项目索引或键。

答案 1 :(得分:1)

变化:

$attrs.currentOrder=order;

为:

$scope.currentOrder=order;