简体   繁体   English

指令角度中的访问范围控制器变量

[英]Access scope controller variable in Directive angular

I want to access Scope variable total in directive link property. 我想在指令链接属性中访问范围变量total

Even though total value is updating on mouseDown event was fired, but the $scope.total was not changing. 即使触发了mouseDown事件时总值正在更新,但$ scope.total仍未更改。

Here is the code 这是代码

functionality: Total amount changes when mouse down on respective boxes 功能:将鼠标移到相应的框上时,总量发生变化

  var app = angular.module("billModule", []); app.controller("billController", function ($scope) { $scope.total = 0.0; }); app.directive("menu", function ($document) { return { restrict: "EA", replace: true, link: function (scope, element, attr) { element.on("mousedown", function () { scope.total += Number(attr.cost); //console.log("total is "+ scope.total); }); }, template: function (element, attr) { return "<div class='box' title='$" + attr.cost + "'>" + attr.item + "</div>"; } } }) 
 .box { width: 132px;height: 38px;background-color: red;cursor: pointer;border: groove;text-align: center;padding-top: 5px;font-size: 33px;color: white; } .box:hover { /*background-color: blue;*/ border: inset; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="billModule"> <div ng-controller="billController"> <menu item="Gold" cost="20"></menu> <menu item="fruits" cost="40"></menu> <menu item="phone" cost="90"></menu> <menu item="water" cost="50"></menu> <menu item="monitor" cost="70"></menu> <div>{{total | currency : '$':2}}</div> </div> </div> 

Angular is not aware of the change, since it was made using an external event handler, and not an angular event handler (ngClick for example). Angular不知道更改,因为它是使用外部事件处理程序而非角度事件处理程序(例如ngClick)进行的。 To make angular aware of the change, wrap it with scope.$apply : 要使角度了解更改,请用scope.$apply包裹它:

scope.$apply(function() {
    scope.total += Number(attr.cost);
});

If you're using angular 1.3 or above, use scope.$applyAsync , as it's a bit more performant. 如果您使用的是angular 1.3或更高版本,请使用scope.$applyAsync ,因为它的性能更高。

Use 'scope.$digest();' 使用'scope。$ digest();' after 'scope.total += Number(attr.cost);' 在'scope.total + = Number(attr.cost);'之后 your total will gets updated in the template. 您的总数将在模板中更新。 Refer below plnkr 请参阅下面的plnkr

http://plnkr.co/edit/nOPggwreTTEukYUMbD1X?p=preview http://plnkr.co/edit/nOPggwreTTEukYUMbD1X?p=预览

Code: 码:

var app = angular.module("billModule", []);
app.controller("billController", function ($scope) {
    $scope.total = 0.0;
});
app.directive("menu", function ($document) {
    return {
        restrict: "EA",
        replace: true,
        link: function (scope, element, attr) {
            element.on("mousedown", function () {
                scope.total += Number(attr.cost);
                scope.$digest();
              //console.log("total is "+ scope.total);
            });
        },
        template: function (element, attr) {

            return "<div class='box' title='$" + attr.cost + "'>" + attr.item + "</div>";
        }
    }
});

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

相关问题 如何在指令链接中访问angular $ scope控制器? - How to access angular $scope controller in directive link? 无法在Angular中的控制器内部访问范围变量 - Unable to access scope variable inside a controller in Angular Angular JS指令控制器访问范围属性,但返回未定义 - Angular js directive controller access scope property but returns undefined 如何在Angular中从控制器访问范围到自定义指令? - How to access scope from controller to custom directive in Angular? Angular:如何在控制器中访问元素指令的作用域 - Angular: How to access an element directive's scope in the controller 在angular中,是否可以在运行控制器之前让指令将变量添加到作用域? - In angular, is it possible to have a directive add a variable to the scope before the controller is run? 在angular.js中的指令中从控制器访问范围变量 - Accessing scope variable from controller in a directive in angular.js Angular + Typescript:在控制器类中使用指令类作用域变量 - Angular + Typescript: Use Directive Class Scope Variable in Controller Class Angular指令控制器作用域继承 - Angular directive controller scope inheritance Angular指令链接函数中的作用域是否与角度指令控制器中的$ scope相同? - Is the scope in an Angular directive link function the same $scope in an angular directive controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM