mapApp.controller("myController", function ($scope,$http) {
$scope.namePlaceHolder= "Name";
$scope.name = "";
};
I bound a scope variable to an html input as follows.
<input id="foo" type="text" placeholder="{{namePlaceHolder}}" ng-model="name" value="{{name}}"/>
If a user types something in text box the $scope.name property changes. But when I change it using javascript the $scope.name data doesn't change.
on(document.getElementById("button"), "click", function (e) {
document.getElementById("foo").value = "ascd...";
})
This code does not populate $scope.name data.
$rootScope is a parent object of all “$scope” angular objects created in a webpage. $scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage. It is created with the ng-app directive. It is created with the ng-controller directive.
The $ in AngularJs is a built-in object.It contains application data and methods.
In Angular 2.0, there will be no $scope .
$scope.$apply() This function is used to execute an expression in Agular. The function expression is optional and you can directly use $apply(). This is used to run watcher for the entire scope. $rootScope.$digest()
Accesing scope from external element:
on(document.getElementById("button"), "click", function (e) {
var scope = angular.element(document.getElementById("foo")).scope();
scope.name = "hello, World!";
})
Accessing and apply scope from external element:
JS:
on(document.getElementById("button"), "click", function (e) {
var scope = angular.element(document.getElementById("foo")).scope();
scope.name = "hello, World!";
scope.$apply();
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With