将对象(变量)传递给指令angularjs

时间:2016-04-18 20:53:02

标签: javascript angularjs

控制器处于状态。

.controller('test', function($scope) {
    $scope.variable = 'Hello';
    $scope.other = 'Hi';
})
.directive('customDir', function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            attrs.customDir += ' Word';
        }
    }
});
<input type="text" custom-dir="variable" />
<div>{{variable}}</div>

<input type="text" custom-dir="other" />
<div>{{other}}</div>

我需要$ scope.variable和$ scope.other接收&#34; Word&#34;,必须能够在Directive中更改传递变量的值。

1 个答案:

答案 0 :(得分:0)

有几种方法。第一个是使用双向绑定:

Temporary folder: /private/tmp/monaca/571550297e2193515015be16
Downloading project
Download complete
Running for 4.1
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;mobi.monaca.plugins.Monaca&quot; for android
cordova version not detected (lacks script &quot;/private/tmp/monaca/571550297e2193515015be16/android/skeleton/platforms/android/cordova/version&quot; ), continuing.
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;org.apache.cordova.splashscreen&quot; for android
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;org.apache.cordova.camera&quot; for android
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;org.apache.cordova.file&quot; for android
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;org.apache.cordova.file-transfer&quot; for android
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;org.apache.cordova.geolocation&quot; for android
No ALTQ support in kernel
ALTQ related functions disabled
pf disabled
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Installing &quot;phonegap-plugin-push&quot; for android
cordova version not detected (lacks script &quot;/private/tmp/monaca/571550297e2193515015be16/android/skeleton/platforms/android/cordova/version&quot; ), continuing.
cordova-android version not detected (lacks script &quot;/private/tmp/monaca/571550297e2193515015be16/android/skeleton/platforms/android/cordova/version&quot; ), continuing.
Error during processing of action! Attempting to revert...
Failed to install &apos;phonegap-plugin-push&apos;:Error: Uh oh!
cannot find &quot;/Applications/adt-bundle-mac/sdk/com.android.support:support-v13:23+/project.properties&quot; referenced in &lt;framework&gt;
    at Object.AndroidProject.addSubProject (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/util/android-project.js:70:51)
    at module.exports.framework.install (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/platforms/android.js:110:27)
    at Object.ActionStack.process (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/util/action-stack.js:72:25)
    at handleInstall (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:591:20)
    at /data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:340:28
    at _fulfilled (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:787:54)
    at self.promiseDispatch.done (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:816:30)
    at Promise.promise.promiseDispatch (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:749:13)
    at /data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:509:49
    at flush (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:108:17)
Error: Uh oh!
cannot find &quot;/Applications/adt-bundle-mac/sdk/com.android.support:support-v13:23+/project.properties&quot; referenced in &lt;framework&gt;
    at Object.AndroidProject.addSubProject (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/util/android-project.js:70:51)
    at module.exports.framework.install (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/platforms/android.js:110:27)
    at Object.ActionStack.process (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/util/action-stack.js:72:25)
    at handleInstall (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:591:20)
    at /data/monaca_build_module/4.1/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:340:28
    at _fulfilled (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:787:54)
    at self.promiseDispatch.done (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:816:30)
    at Promise.promise.promiseDispatch (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:749:13)
    at /data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:509:49
    at flush (/data/monaca_build_module/4.1/node_modules/cordova/node_modules/q/q.js:108:17)
Build error: Error building project source code

演示: http://plnkr.co/edit/zQOMttjnbvdf6OJNOeoq?p=preview

如果您不需要新的隔离范围,则另一个:

.directive('customDir', function() {
    return {
        scope: {customDir: '='},
        link: function(scope, element, attrs) {
            scope.customDir += ' Word';
        }
    }
});

演示: http://plnkr.co/edit/FmIy3z4t1SEJQn1YQAkP?p=preview