correct way to initialise scope values when the view is loaded with angularjs, ngInit?
Date : March 29 2020, 07:55 AM
it helps some times It is bad practice because the view is initialized at a different time than the controller AND the digest cycle has to process that function, which is an unnecessary addition to that cycle. I assume you have something like: View: <div ng-init="init()">
<span>{{thing}}</span>
</div>
Module.controller('viewController', function(scope){
...
var init = function(){
scope.thing = "123";
...
}
...
})
<div>
<span ng-bind="thing"></span>
</div>
Module.controller('viewController', function(scope){
scope.thing = "123";
})
|
Angularjs -> how to initialise variables in angular-style
Date : March 29 2020, 07:55 AM
should help you out I'd probably put the variables in a service, and then inject that into a wrapping angular controller. Any other controllers that you would want to have access to these 'global' variables would be nested under the wrapping controller, thus inheriting the variables. var app = angular.module("app", []);
app.service("globVars", function () {
var vars = {};
vars.someVar = "a";
vars.someOtherVar = "b";
return vars;
});
app.controller("WrappingCtrl", function ($scope, globVars) {
$scope.globVars = globVars;
});
app.controller("NestedCtrl", function ($scope) {
console.log($scope.globVars.someVar); // => "a"
});
<html ng-app="app">
<div id="wrapper" ng-controller="WrappingCtrl">
<div class="nested" ng-controller="NestedCtrl">
{{globVars.someVar}}
</div>
</div>
</html>
|
Can't initialise a controller in AngularJS
Date : March 29 2020, 07:55 AM
To fix this issue You have ng-app set on the head element, so when Angular bootstraps, it's initializing your app in an area where your controller isn't (and can't be) located. This explains why you're seeing the run log; and yet, your controller doesn't work. <html lang="en" ng-app="batchSparta">
|
Initialise a select in AngularJs
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Use As syntax and remove track by. PS : If someone knows why i have to remove track by he can edit my answer because i don't really know why. function theController($scope) {
$scope.coefs = [
{name:'mm', val:1/1000},
{name:'cm', val:1/100},
{name:'m', val:1}
];
$scope.myCoef = 1/100;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>
<body ng-app>
<div ng-controller="theController">
<select ng-model="myCoef"
ng-options="coef.val as coef.name for coef in coefs">
</select>{{myCoef}}
</div>
</body>
|
AngularJS How to Select and Initialise function on page load
Date : March 29 2020, 07:55 AM
|