[英]how to check if ng-init is processed without using $scope in angularjs?
[英]How to pass scope variable in ng-init using AngularJS
我有問題從范圍聲明的變量傳遞給ng-init:
到目前為止我有這樣的事情:
$scope.x = '10';
<div ng-controller = "controller" ng-init="function(x)">
如何從ng-init函數中的范圍傳遞x var?
很多人會告訴你你不應該這樣做(如文檔中所述: https : //docs.angularjs.org/api/ng/directive/ngInit )。
至於實際做......
<div ng-controller="controller" ng-init="x = x">
也許這個可以幫助你:
$scope.x = '10';
<div ng-controller = "controller" ng-init="x">
如果你想使用ng-init推送一個值,我會使用這樣的東西
Js代碼
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.x = "this will be replaced"; //not really needed
$scope.initialize = function(bar){
$scope.x=bar;
}
})
和HTML
<div ng-app="app" ng-controller="ctrl" ng-init="initialize('your value')">
如果你想在你有控制器的標簽上執行一個函數,為什么不簡單地在控制器的構造函數中運行該函數(即函數)?
並非一切都必須在標記中。
JS:
angular.module(...).controller('MyController', function ($scope) {
$scope.myFunction($scope.x);
})
HTML:
<div ng-controller="MyController"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.