changing a controller scope variable in a directive is not reflected in controller function

前端 未结 4 485
傲寒
傲寒 2020-12-16 08:11

In my directive, I have a controller variable, page which gets incremented when you press the button in the directive. However, the next line, scope.alertPage()

4条回答
  •  离开以前
    2020-12-16 09:01

    Try doing this slightly differently. Pass in a function to do the increment rather than incrementing inside the directive

    HTML

            
    

    Controller

              $scope.incPage = function() {    // Function to increment
                 $scope.page++;
             };
    

    In Directive

             scope: {
               page: '&',      // Receive like this
               alertPage: '&'
             },
    
    
              link: function(scope, elem, attrs) {
                   scope.incrementPage = function() {
                     scope.page();          // Call as function
                     scope.alertPage();
                   }  
              }
    

提交回复
热议问题