简体   繁体   English

AngularJS在指令之间共享控制器

[英]Angularjs Sharing controller between directive

While implementing angularjs directive i have got some issues in sharing controller between directive 在实现angularjs指令时,我在指令之间共享控制器时遇到了一些问题

i cant access the enterUser directive from the below controller 我无法从下面的控制器访问enterUser指令

app.directive('entires', [function () {
  return {
    restrict: 'E',
    replace: true,
    scope : {
      user : '='
    },
    require : '^?enterUser',
    template:"<div><b>Time : </b>{{user.name}}  <b>Task :</b> {{user.age}} <a ng-click='delete(user);'><u>Delete Entry</u></a> <br></div>",
    link: function (scope, iElement, iAttrs, enterUserctrl) {
     console.log(ctrl)
    //here i got enterUserctrl undefined..
    // i want like to call delete function from here
    enterUserctrl.delete(user); 


    }

  };
}])

here the current working fiddle 这是当前工作的小提琴

I modified your code a little. 我对您的代码做了一些修改。 Two main errors: enter-user should wrap entires so angular could find it for require . 两个主要错误: enter-user应该包装entires包装,以便有角度的人可以找到require And the second is that you need to use transclude in your case. 其次,您需要在情况下使用transclude

Take a look at the code 看一下代码

app.directive('enterUser', function () {
    return {
        restrict: "A",
        transclude: true,
        templateUrl: 'enter-user.html',

        controller: function ($scope) {

            $scope.addToList = function (name, age) {
                if (typeof $scope.userName != 'undefined' && typeof $scope.userAge != 'undefined') {
                    $scope.nameList.push({
                        name: $scope.userName,
                        age: $scope.userAge
                    })
                    $scope.userName = '';
                    $scope.userAge = '';
                }
            };

            this.delete = function(user) {
                if (typeof user != 'undefined') {
                    $scope.nameList.pop();
                }
            };
        }
    };
});

enter-user.html 进入-user.html

<div>
    <b>Name: </b>
    <input ng-model='userName' type='text' />
    <br>

    <b>Age  : </b> 
    <input ng-model='userAge' type='text' />
    <br>

    <span class='right'><button ng-click='addToList(userName, userAge);'>Add to List</button></span>

    <!-- insert trascluded content here -->
    <div ng-transclude></div>
</div>

entires directive 整体指令

app.directive('entires', function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            user: '='
        },
        require: '^enterUser',
        templateUrl: "entires.html",
        link: function (scope, iElement, iAttrs, enterUserCtrl) {
            scope.delete = function(user) {
                enterUserCtrl.delete(user)
            }
        }
    };
});

index.html 的index.html

<div enter-user>
    <b><u>Here is my entries listed </u></b>
    <div ng-repeat="user in nameList">
        <entires user="user"></entires>
        <br>
    </div>
</div>

Demo Plunker 演示柱塞

Also your delete function does not work properly. 此外,您的删除功能无法正常工作。 But this is little thing. 但这不是小事。

From your code the <div enter-user></div> is separated from second directive entires . 从您的代码中, <div enter-user></div>与第二个指令entires

If entires directive uses parent directive enterUser the structure I think should be something like: 如果entires指令使用指令enterUser则我认为结构应类似于:

 <div enter-user>
      <div ng-repeat="user in nameList track by $index">
          <entires user="user"></entires>
      </div>
   </div>

You can see THIS demo that might help you. 您可以看到演示可能会对您有所帮助。

^ – Will look for the directive on parent elements, if not available on the same element. ^ –如果在同一元素上不可用,将在父元素上寻找指令。

From child directive: 从子指令:

require : '^?enterUser',

If we remove ? 如果我们删除? we will get an error that parent directive not found. 我们会得到一个错误,找不到父指令。 So this is a issue. 所以这是一个问题。

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

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