首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能从angularJs指令中获得控制器的功能

不能从angularJs指令中获得控制器的功能
EN

Stack Overflow用户
提问于 2014-09-15 15:41:06
回答 1查看 128关注 0票数 0

我试图在内部编辑之后在我的控制器中运行一个函数来更新我的底层数据,但是它不会触发.不明白为什么。我怎么才能检查我是否在正确的范围内?我在用Meanjs

作为测试,我试图在控制器中触发testar()函数。

代码语言:javascript
复制
angular.module('customers').directive('editInPlace', [
function() {
    return {
        restrict: 'E',
        scope: {
            value: '='
        },
        template: '<span ng-click="edit()" ng-bind="value"></span><input ng-model="value"></input>',
        link: function (scope, element, attrs) {
            // Let's get a reference to the input element, as we'll want to reference it.
            var inputElement = angular.element(element.children()[1]);

            // This directive should have a set class so we can style it.
            element.addClass('edit-in-place');

            // Initially, we're not editing.
            scope.editing = false;

            // ng-click handler to activate edit-in-place
            scope.edit = function () {
                scope.editing = true;

                // We control display through a class on the directive itself. See the CSS.
                element.addClass('active');

                // And we must focus the element.
                // `angular.element()` provides a chainable array, like jQuery so to access a native DOM function,
                // we have to reference the first element in the array.
                inputElement[0].focus();
            };

            // When we leave the input, we're done editing.
            inputElement.prop('onblur', function () {
                scope.editing = false;
                element.removeClass('active');
                console.log("trigg");
                scope.$apply("testar()");
            });
        }
    };
}

]);

代码语言:javascript
复制
angular.module('customers').controller('CustomersController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
function($scope, $stateParams, $location, Authentication, Customers ) {
    $scope.authentication = Authentication;

    // Create new Customer
    $scope.create = function() {
        // Create new Customer object
        var customer = new Customers ({
            name: this.name,
            firstName:this.firstName
        });

    $scope.testar = function () {
        console.log("jojjemen från controller");
    }

}

]);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-15 15:49:34

添加到您的范围:

代码语言:javascript
复制
scope
{
  value: '=',
  controllerFunction: '='
}

在您调用的链接函数上:

代码语言:javascript
复制
   scope.controllerFunction();

然后在HTML上,当包含指令时,需要将控制器函数作为属性传递:

代码语言:javascript
复制
<edit-in-place value="something" controller-function="testar">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25851827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档