jquery 呼叫ng function與更新變數
by ken tsai
HTML
<div id="span" ng-app='MyModule' ng-controller="MyController">
{{mytext}}
</div>
JavaScript
angular.module('MyModule', [])
.controller('MyController', function ($scope) {
$scope.mytext = "Hello World"; //default
$scope.myfunction = function (data) {
alert("---" + data);
};
$scope.$watch('text', function(newValue, oldValue) {
console.log(newValue);
console.log(oldValue);
});
});
window.onload = function () {
//get ng scope by elem id
var ngScope = angular.element(document.getElementById('span')).scope();
//trigger ng-function by jQuery
ngScope.myfunction('test');
//change mytext parameter
ngScope.mytext = 'Hello World 2';
//update mytext to angular world with $apply function
ngScope.$apply(function(){
});
}