I am trying to create some custom elements with AngularJS and bind some events to it, then I noticed that $ scope.var will not update the interface when used in the binding function.
Here is a simplified example describing a task:
HTML:
<!doctype html> <html ng-app="test"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <script src="script.js"></script> </head> <body> <div ng-controller="Ctrl2"> <span>{{result}}</span> <br /> <button ng-click="a()">A</button> <button my-button>B</button> </div> </body> </html>
JS:
function Ctrl2($scope) { $scope.result = 'Click Button to change this string'; $scope.a = function (e) { $scope.result = 'A'; } $scope.b = function (e) { $scope.result = 'B'; } } var mod = angular.module('test', []); mod.directive('myButton', function () { return function (scope, element, attrs) {
DEMO: http://plnkr.co/edit/g3S56xez6Q90mjbFogkL?p=preview
Basically, I bind the click event to my-button and want to change $scope.result when the user presses button B (similar to ng-click:a() on button A). But if I do so, the view will not be updated to the new $scope.result .
What did I do wrong? Thank.
javascript angularjs
Mark Ni Apr 17 '13 at 17:16 2013-04-17 17:16
source share