简体   繁体   English

从指令调用控制器功能不会更新作用域

[英]Calling controller function from directive does not update scope

I made a directive using a service and $resource to search a REST API and pass the result to a controller. 我使用服务和$ resource进行了指令搜索REST API,并将结果传递给控制器​​。 The code looks like this: 代码如下:

app.controller('productsCtrl', ['$scope', function ($scope) {
    var self = this;
    self.selection;

    self.searched = function (selected) {
        self.selection = selected;
    };
}]);

app.service('productsService', ['productsResource', function (productsResource) {
    var self = this;

    self.findProducts = function (search) {
        return productsResource.query({
            sku: search
        });
    };
}]);

app.factory('productsResource', ['$resource', function ($resource) {
    return $resource('products/:sku', {
        id: '@sku'
    });
}]);

app.directive('productSearch', ['productsService', function (productsService) {
    return {
        restrict: 'E',
        scope: {
            productSelected: '&'
        },
        templateUrl: '/products/product_search.html',
        link: function (scope) {
            scope.findProducts = function (search) {
                scope.searchResults = productsService.findProducts(search);
            };
            scope.selectResult = function (selected) {
                scope.selection = selected;
                scope.showResults = false;
            };
        }
    };
}]);

The relevant HTML lines are: 相关的HTML行是:

<div ng-show='products.selection'>
    {{products.selection.name}}  //does not update
</div>

and from the directives template: 并从指令模板中:

<div ng-click="productSelected({selected: selection})"></div>

The problem is my model does not update when the directive calls the controller method (it will show the results of the last search). 问题是当指令调用controller方法时,我的模型没有更新(它将显示上一次搜索的结果)。 It sort of makes sense to me because no "angular" function is being called in there. 这对我来说很有意义,因为其中没有“角度”函数被调用。 But how am I supposed to get this to work. 但是我应该如何使它工作。 I've tried with $scope.$apply() but that throws an error. 我试过$scope.$apply()但这会引发错误。

As a side question. 作为附带问题。 Is the way I'm doing it right? 我做的方式正确吗? Is this the angular way? 这是成角度的方式吗? Especially using a service inside the directive and passing things from the directive to a controller? 特别是在指令内部使用服务并将指令中的内容传递给控制器​​吗?

EDIT: This is the whole HTML file (using controllerAs): 编辑:这是整个HTML文件(使用controllerAs):

<div>
    <product-search product-selected='products.searched(selected)'></product-search>
    <div ng-show='products.selection'>
        {{products.selection.name}}
    </div>
</div>

I think you are missing the callback from directive. 我认为您缺少指令中的回调。 You might need to add 您可能需要添加

scope.productSelected({selected: selection})

inside the scope.selectResult function in directive. 在指令的scope.selectResult函数中。 The full code will look like this 完整的代码如下所示

 scope.selectResult = function (selected) {
     scope.selection = selected;
     scope.showResults = false;
     scope.productSelected({selected: selected}); // add this
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM